HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
Barricade
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 997 Accepted Submission(s): 306Problem DescriptionThe 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.
InputThe 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(N≤1000) and M(M≤10000).
The i-the line of the next M lines describes the i-th edge with three integers u,v and w where 0≤w≤1000 denoting an edge between u and v of barricade cost w.
OutputFor each test cases, output the minimum wood cost.
Sample Input1 4 4 1 2 1 2 4 2 3 1 3 4 3 4
Sample Output4
Source
Recommendwange2014
Statistic | Submit | Discuss | Note
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5889
题目大意:
N(N<=1000)个城市,你在城市1,敌人在城市N,敌人会选择从N到1的最短路进攻,你需要在某些边上放障碍来阻挡敌人进攻。
总共有M(M<=1000)条无向边,连接两个城市,距离都为1,放置障碍的费用为wi。求最小费用。
题目思路:
【BFS+最小割】
首先因为每条边的距离都是1,所以先从N开始往1跑最短路,扩展所有距离d[u]<=d[1]的点,在最短路过程中,对于u->v的边,在新图上加一条v->u的容量为wi的边。
这样从N跑完一次BFS之后建的新图是从1到N的一张最短路图。问题转化为求新图的最小割。从1到N开始跑最大流即可。
1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #include<iomanip> 8 #include<map> 9 #include<stack> 10 #include<queue> 11 #include<set> 12 #include<bitset> 13 #include<memory.h> 14 #include<time.h> 15 #include<stdio.h> 16 #include<stdlib.h> 17 #include<string.h> 18 //#include<stdbool.h> 19 #include<math.h> 20 #define min(a,b) ((a)<(b)?(a):(b)) 21 #define max(a,b) ((a)>(b)?(a):(b)) 22 #define abs(a) ((a)>0?(a):(-(a))) 23 #define lowbit(a) (a&(-a)) 24 #define sqr(a) ((a)*(a)) 25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) 26 #define mem(a,b) memset(a,b,sizeof(a)) 27 #define eps (1e-10) 28 #define J 10000 29 #define mod 1000000007 30 #define MAX 0x7f7f7f7f 31 #define PI 3.14159265358979323 32 #pragma comment(linker,"/STACK:1024000000,1024000000") 33 #define N 1004 34 #define M 10004 35 using namespace std; 36 typedef long long LL; 37 double anss; 38 LL aans; 39 int cas,cass; 40 int n,m,lll,ans; 41 int nn,S,T; 42 int d[N],vd[N],last[N],last1[N]; 43 bool u[N]; 44 struct xxx 45 { 46 int next,to,q; 47 }a[M<<1],b[M<<1]; 48 void add(int x,int y,int z) 49 { 50 a[++lll].next=last[x]; 51 a[lll].to=y; 52 a[lll].q=z; 53 last[x]=lll; 54 } 55 void link(int x,int y,int z) 56 { 57 b[++cas].next=last1[x]; 58 b[cas].to=y; 59 b[cas].q=z; 60 last1[x]=cas; 61 } 62 int sap(int u,int f) 63 { 64 int i,v,tt,asp=0,mix=nn-1; 65 if(u==T)return f; 66 for(i=last[u];i;i=a[i].next) 67 { 68 v=a[i].to; 69 if(a[i].q>0) 70 { 71 if(d[u]==d[v]+1) 72 { 73 tt=sap(v,min(f-asp,a[i].q)); 74 asp+=tt; 75 a[i].q-=tt; 76 a[i^1].q+=tt; 77 if(asp==f || d[S]==nn) 78 return asp; 79 } 80 mix=min(mix,d[v]); 81 } 82 } 83 if(asp!=0)return asp; 84 if(!--vd[d[u]])d[S]=nn; 85 else vd[d[u]=mix+1]++; 86 return asp; 87 } 88 void bfs() 89 { 90 int now,to,i; 91 queue<int>q; 92 mem(d,MAX); 93 u[n]=1;q.push(n);d[n]=0; 94 while(!q.empty()) 95 { 96 now=q.front();q.pop(); 97 if(d[now]>=d[S])continue; 98 for(i=last1[now];i;i=b[i].next) 99 { 100 to=b[i].to; 101 if(d[now]+1>d[to])continue; 102 d[to]=d[now]+1; 103 add(now,to,0),add(to,now,b[i].q); 104 if(!u[to]) 105 { 106 u[to]=1; 107 if(to!=S)q.push(to); 108 } 109 } 110 } 111 mem(d,0); 112 } 113 int main() 114 { 115 #ifndef ONLINE_JUDGEW 116 freopen("1.txt","r",stdin); 117 // freopen("2.txt","w",stdout); 118 #endif 119 int i,j,k; 120 int x,y,z,f; 121 // init(); 122 for(scanf("%d",&cass);cass;cass--) 123 // for(scanf("%d",&cas),cass=1;cass<=cas;cass++) 124 // while(~scanf("%s",s)) 125 // while(~scanf("%d",&n)) 126 { 127 lll=cas=1;ans=0; 128 mem(u,0);mem(vd,0);mem(last,0);mem(last1,0); 129 scanf("%d%d",&n,&m); 130 for(i=1;i<=m;i++) 131 { 132 scanf("%d%d%d",&x,&y,&z); 133 link(x,y,z),link(y,x,z); 134 } 135 nn=n; 136 S=1,T=n; 137 vd[0]=nn; 138 bfs(); 139 while(d[S]<nn) 140 { 141 f=sap(S,MAX); 142 ans+=f; 143 } 144 printf("%d\n",ans); 145 } 146 return 0; 147 } 148 /* 149 // 150 151 // 152 */