HDU 5889 Barricade 最小割

来源:互联网 发布:5g网络架构 杨峰义pdf 编辑:程序博客网 时间:2024/05/18 17:02

Barricade

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1699    Accepted Submission(s): 501


Problem Description
The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as N towns and M roads, and each road has the same length and connects two towns. The town numbered 1 is where general's castle is located, and the town numbered N 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 i-th road requires wi 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 t, then t test cases follow.
For each test case, in the first line there are two integers N(N1000) and M(M10000).
The i-the line of the next M lines describes the i-th edge with three integers u,v and w where 0w1000 denoting an edge between u and v of barricade cost w.
 

Output
For each test cases, output the minimum wood cost.
 

Sample Input
14 41 2 12 4 23 1 34 3 4
 

Sample Output
4
 

Source
2016 ACM/ICPC Asia Regional Qingdao Online


一个无向图,给你n个点m条边,每条边的长度相同。让你将图上的最短路封住。封住每条路有一个代价,求最小代价。


先bfs一遍求最短路,标记使用过的路径之后,直接上最小割。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=1005,maxk=10005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);  int head[maxn],step[maxn],x[maxk],y[maxk],d[maxk];int dist[maxn],current[maxn];bool visit[maxn],mark[maxk*2];int num; struct Edge {int from,to,flow,pre;};Edge edge[maxk*2];void addedge(int from,int to,int flow) {edge[num]=(Edge){from,to,flow,head[from]};head[from]=num++;edge[num]=(Edge){to,from,0,head[to]};head[to]=num++;}bool bfs (int n) {queue<int> q;q.push(1);memset(dist,-1,sizeof(dist));dist[1]=0;while (!q.empty()) {int now=q.front();q.pop();for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (dist[to]==-1&&edge[i].flow>0) {dist[to]=dist[now]+1;q.push(to);}}}return dist[n]!=-1;}int dfs(int now,int flow,int n) {int f;if (now==n) return flow;for (int i=current[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;current[now]=i;if (dist[now]+1==dist[to]&&edge[i].flow>0&&(f=dfs(to,min(flow,edge[i].flow),n))) {edge[i].flow-=f;edge[i^1].flow+=f;return f;}}return 0;}int dinic(int n) {int sum=0,f;while (bfs(n)) {memcpy(current,head,sizeof(head));while (f=dfs(1,inf,n)) sum+=f;}return sum;}int main() {int cas;scanf("%d",&cas);while (cas--) {int n,m,i,j;num=0;memset(head,-1,sizeof(head));scanf("%d%d",&n,&m);for (i=1;i<=m;i++) {scanf("%d%d%d",&x[i],&y[i],&d[i]);addedge(x[i],y[i],d[i]);}mem0(visit);queue<int> q;q.push(1);step[1]=0;visit[1]=1;while (!q.empty()) {int now=q.front();q.pop();for (i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) {visit[to]=1;step[to]=step[now]+1;q.push(to);}}}mem0(mark);for (i=1;i<=n;i++) {for (j=head[i];j!=-1;j=edge[j].pre) {int to=edge[j].to,from=edge[j].from;if (step[to]==step[from]+1) mark[j]=1;}}num=0;memset(head,-1,sizeof(head));for (i=0;i<2*m;i+=2) {if (mark[i]) addedge(x[i/2+1],y[i/2+1],d[i/2+1]);if (mark[i+1]) addedge(y[i/2+1],x[i/2+1],d[i/2+1]);}int ans=dinic(n);printf("%d\n",ans);}return 0;}

原创粉丝点击