【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 4Sample Output
4題目大意:
我在1號(hào)點(diǎn),敵人在n號(hào)點(diǎn),敵人會(huì)走n到1的最短路徑進(jìn)攻你了,現(xiàn)在你需要在路徑上放置障礙,但是每條邊上放置障礙有一個(gè)花費(fèi),求能阻擋所有敵人放置最少花費(fèi)的障礙。
解題報(bào)告:
因?yàn)檫叺拈L(zhǎng)度都是1,所以直接bfs求最短路,然后建出最短路子圖,然后求最小割就行了。題目思路很清晰,算是一道比較簡(jiǎn)單的網(wǎng)絡(luò)流。
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];//一共多少個(gè)點(diǎn)跑bfs,dis數(shù)組和q數(shù)組就開多大。 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為源點(diǎn)到這個(gè)點(diǎn)的路徑上的最小邊權(quán) 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*/?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【HDU - 5889】Barricade(最短路+网络流,最小割)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《三国群英传8》武将专属技能、立绘、头像
- 下一篇: 【牛客 - 303H第十五届浙江大学宁波