多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > HDU-5889-Barricade【2016青島網絡】【spfa】【最小割】

HDU-5889-Barricade【2016青島網絡】【spfa】【最小割】

來源:程序員人生   發布時間:2017-01-13 10:50:13 閱讀次數:2686次

5889-Barricade


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(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.

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

題目鏈接:HDU⑸889

題目大意:給出n個點,m條路徑,每條路徑長度為1,敵人從m點攻擊1點,敵人總是選擇最短路徑來進攻我方,為了禁止敵人,我們要把1些路封死,每條路徑封死需要1些花費,求最小花費。

題目思路:首先用spfa處理出最短路中的邊,然后做1遍最大流求出最小割。

可參考HDU⑶416

以下是代碼:

#include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <cmath> #include <cstdio> #include <cstring> #include <cctype> #include <algorithm> #include <functional> #include <numeric> #include <string> #include <set> #include <map> #include <stack> #include <vector> #include <queue> #include <deque> #include <list> using namespace std; #define LL long long #define MAXN 1005 struct node { int v,len,w; //終點、長度 node(int a,int b,int c){ v = a,len = b,w = c; } }; struct edge { int to,cap,rev; edge(int a,int b,int c){ to = a,cap = b,rev = c; } }; int n; vector <node> g[MAXN]; vector <edge> G[MAXN]; int d[MAXN]; bool used[MAXN];//DFS中用到的訪問標記 void addEdge(int u,int v,int cap) { G[u].push_back(edge(v,cap,G[v].size())); G[v].push_back(edge(u,0,G[u].size()-1)); } //通過DFS尋到增廣路 int dfs(int v,int t,int f){ if(v == t) return f; used[v] = true; for(int i = 0; i < G[v].size(); i++){ edge &e = G[v][i]; if(!used[e.to] && e.cap > 0){ int d = dfs(e.to,t,min(f,e.cap)); if(d > 0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } //求解從s到t的最大流 int max_flow(int s,int t){ int flow = 0; while(1){ memset(used,false,sizeof(used)); int f = dfs(s,t,99999999); if(f == 0) return flow; flow += f; } } void solve() { for (int i = 1; i <= n; i++) { int len = g[i].size(); for (int j = 0; j < len; j++) { int v = g[i][j].v; int l = g[i][j].len; int w = g[i][j].w; if (d[v] - d[i] == l) //判斷是不是為最短路 { addEdge(i,v,w); } } } } void SPFA(int x) //x為出發點 { int v[MAXN]; memset(v,0,sizeof(v)); queue<int>q; q.push(x); v[x] = 1;d[x] = 0; while(!q.empty()) { int nod = q.front(); q.pop(); v[nod] = 0; for(int i = 0;i < g[nod].size();i++) { int nxtnod = g[nod][i].v; if(d[nxtnod] > d[nod] + g[nod][i].len) { d[nxtnod] = d[nod] + g[nod][i].len; if(!v[nxtnod]) { v[nxtnod] = 1; q.push(nxtnod); } } } } } void init() { memset(d,1,sizeof(d)); for (int i = 0; i < MAXN-1; i++) { g[i].clear(); G[i].clear(); } } int main() { int t; cin >> t; while(t--) { int m; cin >> n >> m; init(); for (int i = 0; i < m; i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); g[u].push_back(node(v,1,w)); g[v].push_back(node(u,1,w)); } SPFA(1); solve(); long long ans = max_flow(1,n); printf("%lld\n",ans); } return 0; }
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 一级秋霞理论 | 一二三四视频免费观看在线看1 | 日本不卡一区二区三区在线观看 | 久久久不卡国产精品一区二区 | 国产一区二区三区高清 | 最新国产一区二区精品久久 | 三级在线国产 | 一区二区精品在线观看 | 国产91精品高清一区二区三区 | 日本中文字幕在线播放 | 天堂最新版免费观看 | 久久亚洲精品一区成人 | 久久久一区二区三区 | 日本在线国产 | 91精品国产综合久久欧美 | videos雌雄同体xxxx视频 | 天天在线天天综合网色 | 欧美hdxxx| 91精品国产亚洲爽啪在线影院 | 欧美性videosex18 | 国产日产欧美精品一区二区三区 | 一级一毛片a级毛片欧美 | 最近中文字幕免费6 | 日本一级毛片视频无遮挡免费 | 国产免费高清在线精品一区 | 久久精品免费一区二区视 | 日本成人性视频 | 亚洲天堂二区 | 亚洲人成影院在线高清 | 禁18在线观看 | 男女同房视频免费爽爽 | 久久天天躁夜夜躁狠狠躁2020 | 午夜在线免费视频 | 激情欧美成人久久综合小说 | 日本中文字幕在线观看视频 | 香蕉视频在线网址 | 亚洲 图片 小说 欧美 另类 | 日本一区视频在线观看 | 午夜性色福利视频 | 国产一级淫片a视频免费观看 | 欧美日韩欧美日韩 |