日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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*/

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的【HDU - 5889】Barricade(最短路+网络流,最小割)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。