【HDU - 5889】Barricade(最短路+网络流,最小割)

题干:

The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as NN towns and MM roads, and each road has the same length and connects two towns. The town numbered 11 is where general's castle is located, and the town numbered NN is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the ii-th road requires wiwi units of wood. Because of lacking resources, you need to use as less wood as possible.

Input

The first line of input contains an integer tt, then tt test cases follow. 
For each test case, in the first line there are two integers N(N≤1000)N(N≤1000) and M(M≤10000)M(M≤10000). 
The ii-the line of the next MM lines describes the ii-th edge with three integers u,vu,vand ww where 0≤w≤10000≤w≤1000 denoting an edge between uu and vv of barricade cost ww.

Output

For each test cases, output the minimum wood cost.

Sample Input

1
4 4
1 2 1
2 4 2
3 1 3
4 3 4

Sample Output

4

题目大意:

我在1号点,敌人在n号点,敌人会走n到1的最短路径进攻你了,现在你需要在路径上放置障碍,但是每条边上放置障碍有一个花费,求能阻挡所有敌人放置最少花费的障碍。

解题报告:

因为边的长度都是1,所以直接bfs求最短路,然后建出最短路子图,然后求最小割就行了。题目思路很清晰,算是一道比较简单的网络流。

AC代码:

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int MAX = 2e5 +5;
int n;
int tot,TOT;
struct Edge {int to,ne,w;
} e[100005 * 2];
struct EE {int fr,to,ne,w;
}E[100005 +5];
int head[10005],HEAD[10005];
int st,ed;
int dis[10050],q[10005];//一共多少个点跑bfs,dis数组和q数组就开多大。 
void add(int u,int v,int w) {e[++tot].to=v;e[tot].w=w;e[tot].ne=head[u];head[u]=tot;
}
void addE(int u,int v,int w) {E[++TOT].to=v;E[TOT].fr = u;E[TOT].w=w;E[TOT].ne=HEAD[u];HEAD[u]=TOT;
}
bool bfs(int st,int ed) {memset(dis,-1,sizeof(dis));int front=0,tail=0;q[tail++]=st;dis[st]=0;while(front<tail) {int cur = q[front];if(cur == ed) return 1;front++;for(int i = head[cur]; i!=-1; i = e[i].ne) {if(e[i].w&&dis[e[i].to]<0) {q[tail++]=e[i].to;dis[e[i].to]=dis[cur]+1;}}}if(dis[ed]==-1) return 0;return 1;
}
int dfs(int cur,int limit) {//limit为源点到这个点的路径上的最小边权 if(limit==0||cur==ed) return limit;int w,flow=0;for(int i = head[cur]; i!=-1; i = e[i].ne) {		if(e[i].w&&dis[e[i].to]==dis[cur]+1) {w=dfs(e[i].to,min(limit,e[i].w));e[i].w-=w;e[i^1].w+=w;flow+=w;limit-=w;if(limit==0) break;}}if(!flow) dis[cur]=-1;return flow;
}
int dinic() {int ans = 0;while(bfs(st,ed)) ans+=dfs(st,0x7fffffff);return ans;
}
int DIS[MAX];
int vis[MAX];
struct Point {int pos,step;Point(){}Point(int pos,int step):pos(pos),step(step){}
}; 
int flag[MAX];
void bfs() {for(int i = 1; i<=n; i++) vis[i] = 0,DIS[i] = -1;queue<Point> q;q.push(Point(1,0));vis[1] = 1;while(!q.empty()) {Point cur = q.front();q.pop();
//		if(vis[cur.pos] == 1) continue;for(int i = HEAD[cur.pos]; ~i; i = E[i].ne) {int v = E[i].to;if(vis[v] == 1) {if(cur.step+1 == DIS[v]) {flag[i] = 1;}}else {DIS[v] = cur.step+1;vis[v] = 1;flag[i] = 1;q.push(Point(v,DIS[v]));}}}
}
int main() 
{int t,m;cin>>t;while(t--) {scanf("%d%d",&n,&m);//inittot=1,TOT=0;for(int i = 0; i<=2*m; i++) flag[i] = 0;for(int i = 1; i<=n; i++) head[i] = HEAD[i] = -1;		for(int u,v,w,i = 1; i<=m; i++) {scanf("%d%d%d",&u,&v,&w);addE(u,v,w);addE(v,u,w);} bfs();for(int i = 1; i<=TOT; i++) {if(flag[i] == 1) {add(E[i].fr,E[i].to,E[i].w);add(E[i].to,E[i].fr,0);}}st=1,ed=n;printf("%d\n",dinic());	}return 0;
}
/*
3
4 4
1 2 1
2 4 2
3 1 3
4 3 46 7
1 2 1
2 6 7
1 3 2
2 4 5
4 6 5
1 5 4
5 6 34 4
1 2 1
2 4 2
3 1 3
4 3 4*/

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440219.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SM4国密对称算法源码解析

最近在研究国密算法&#xff0c;主要分为&#xff1a;SM2、SM3、SM4。其中SM2为非对称加密算法&#xff0c;SM3为哈希摘要算法&#xff0c;SM4为对称加密算法。 1.在SM4算法源文件中主要有以下几个函数&#xff1a; void sm4_setkey_enc( sm4_context *ctx, unsigned char ke…

Mac系统容易忽视但很实用的命令整理

001.终端指定用哪个软件打开指定的文件 # open -a 实用哪个软件(软件名有空格需转义) 文件名路径 open -a /Applications/Google\ Chrome.app xx.html # 【tips:】由于先输入"open -a"后tab键自动补全容易卡死终端&#xff0c;所以推荐先输入"/App..."&a…

【HDU 4394】Digital Square(bfs,数位搜索,思维,数学)

题干&#xff1a; Given an integer N,you should come up with the minimum nonnegative integer M.M meets the follow condition: M 2%10 xN (x0,1,2,3....) Input The first line has an integer T( T< 1000), the number of test cases. For each case, each line…

SM4算法原理

前面的文章介绍了SM4算法的C语言实现&#xff0c;源码可见文章&#xff1a;SM4国密对称算法源码解析_10点43的博客-CSDN博客_sm4代码。 本文将会介绍SM4算法原理&#xff0c;这部分可能会比较枯燥&#xff0c;但数学要求也不是太高。 目录 1.概述 2. 参数产生 3. 轮函数 4…

webpack打包前端项目入门

前言&#xff1a;在开发过程中&#xff0c;利用webpack可以帮我们自动把ES6语法编译成低版本浏览器能解析的JavaScript代码。下面给出webpack打包前端项目入门案例。 [终端&#xff1a;进入项目所在目录] 1.初始化项目依赖&#xff1a; npm init -y 2.安装webpack-cli脚手架…

网络编程懒人入门(八):手把手教你写基于TCP的Socket长连接

转自即时通讯网&#xff1a;http://www.52im.net/ 本文原作者&#xff1a;“水晶虾饺”&#xff0c;原文由“玉刚说”写作平台提供写作赞助&#xff0c;原文版权归“玉刚说”微信公众号所有&#xff0c;即时通讯网收录时有改动。 1、引言 好多小白初次接触即时通讯&#xff…

【HihoCoder - 1502】最大子矩阵(二维前缀和,尺取)

题干&#xff1a; 给定一个NxM的矩阵A和一个整数K&#xff0c;小Hi希望你能求出其中最大&#xff08;元素数目最多&#xff09;的子矩阵&#xff0c;并且该子矩阵中所有元素的和不超过K。 Input 第一行包含三个整数N、M和K。 以下N行每行包含M个整数&#xff0c;表示A。 对…

Idea Spring Boot配置文件.yaml或.properties不能自动提示的有效解决办法

SpringBoot项目的配置文件.yaml/.yml/.properties文件编写的时候没有自动提示&#xff0c;网上的解决办法五花八门&#xff0c;不一定适合具体个人的IDE环境&#xff0c;下面总结一套能解决绝大部分情况的方案&#xff1a; 先给出能自动识别的图样&#xff1a; 步骤1&#xff…

[通俗易懂]深入理解TCP协议(上):理论基础

转自即时通讯网&#xff1a;http://www.52im.net/ 前言 TCP是一个巨复杂的协议&#xff0c;因为他要解决很多问题&#xff0c;而这些问题又带出了很多子问题和阴暗面。所以学习TCP本身是个比较痛苦的过程&#xff0c;但对于学习的过程却能让人有很多收获。关于TCP这个协议的细…

【POJ - 3616】Milking Time (贪心+dp)

题干&#xff1a; Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible. …

Thymeleaf模板引擎处理日期输入框回显问题type=“date“类型的坑 和 单选按钮、复选框的回显

type"date"类型的日期输入框的默认格式为"yyyy/MM/dd"&#xff0c;但是如果使用Thymeleaf的日期格式化工具类的时候使用"yyyy/MM/dd"就无法回显数据&#xff0c;必需使用类似于"yyyy-MM-dd"这种格式才能回显。 <!--emp.birth为Dat…

SM3密码杂凑算法源码解析

1.在SM3算法源文件中主要有以下几个函数&#xff1a; void sm3_starts( sm3_context *ctx ); void sm3_update( sm3_context *ctx, unsigned char *input, int ilen ); void sm3_finish( sm3_context *ctx, unsigned char output[32] ); void sm3( unsigned char *input, int …

【HDU - 5418】Victor and World(tsp旅行商问题,状压dp,floyd最短路,图论)

题干&#xff1a; After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are nn countries on the earth, which are numbered from 11 to nn. T…

Anaconda中软件库更新

今天在Anaconda运行Visualization of MLP weights on MNIST源码时出现了如图错误&#xff1a; 提示无法导入fetch_openml&#xff0c;查了一下是对应的sklearn软件包版本过低&#xff0c;为0.17版。需要更新到0.20版。 1.打开Anaconda Prompt命令行 输入 conda list 命令 查看…

Mac安装mysql8.x最简洁的步骤,避免采坑

1.下载mysql8的.dmg安装包&#xff08;官网下载需要Oracle账号&#xff0c;推荐网上搜索一个8系列的版本即可&#xff09; 2.双击.dmg安装包&#xff0c;不断点击下一步。但是需要注意以下两点&#xff1a; &#xff08;1&#xff09;密码认证方式都选第二个&#xff08;不是…

【HDU - 4784】Dinner Coming Soon(记忆化搜索bfs,dp)

题干&#xff1a; Coach Pang loves his boyfriend Uncle Yang very much. Today is Uncle Yang’s birthday, Coach Pang wants to have a romantic candlelit dinner at Uncle Yang’s house and he has to arrive there in T minutes.   There are N houses in their cit…

Linux操作系统CentOS7安装

最近在学习Linux&#xff0c;今天记录下如何安装CentOS7操作系统。 1. 下载虚拟机软件 虚拟机选择的是VMware Workstation软件&#xff0c;可以访问这个链接下载&#xff1a;https://coding.net/u/aminglinux/p/resource/git/blob/master/README.md 2. 安装虚拟机 按照提示&…

div内容居中和布局居中样式总结

1.div的内容居中 &#xff08;1&#xff09;水平居中 <div align"center"><button>按钮></button></div> CSS&#xff1a; div{display: block; // div默认为块级元素&#xff0c;如果是第三方控件或继承父类元素不是block&#xff…

【HDU - 6349】三原色图(最小生成树,思维,tricks)

题干&#xff1a; 度度熊有一张 nn 个点 mm 条边的无向图&#xff0c;所有点按照 1,2,⋯,n1,2,⋯,n 标号&#xff0c;每条边有一个正整数权值以及一种色光三原色红、绿、蓝之一的颜色。 现在度度熊想选出恰好 kk 条边&#xff0c;满足只用这 k 条边之中的红色边和绿色边就能使…

机器学习初学者公众号下载资源汇总(一)

感谢黄海广博士的分享 原创&#xff1a; 机器学习初学者 机器学习初学者 今天 本站提供了大量的机器学习初学者下载资源&#xff0c;现在对已经公布的资源做下汇总&#xff0c;每个资源都会有一个百度云链接&#xff0c;并同时提供“自动回复”的功能&#xff08;有时候百度云链…