hdoj 5294 Tricks Device 【最短路+ 最小割】

来源:互联网 发布:通甲优博 知乎 编辑:程序博客网 时间:2024/05/19 18:41

Tricks Device

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1889    Accepted Submission(s): 486


Problem Description
Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.
 

Input
There are multiple test cases. Please process till EOF.
For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0<li<=100) minute to pass channel i.
The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.
 

Output
Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
 

Sample Input
8 91 2 22 3 22 4 13 5 34 5 45 8 11 6 26 7 57 8 1
 

Sample Output
2 6
 

题意:给你N个点和M条无向边。现在A需要从点1追在点N的B,他必须走1->N的最短路才可以追上B,问你1:B最少需要破坏几条路才可以阻断最短路;2:B最多破坏多少条路A依旧有希望追上他。


思路:

问题1:SPFA跑一遍最短路,然后构建新图,把最短路的边加进新图边权值为1,在新图求最小割。转化成对立问题,求1 -> N的最大流。

问题2:在SPFA跑最短路的时候,用一个数组记录到达最短路途中每个点的所需要经过的最少边数。最后用M - rec[ N ]就行了。

一开始建图时,建成单向图了,结果WA了一次。。。


AC代码:

#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <stack>#include <algorithm>#define MAXN 2000+100#define MAXM 200000+100#define INF 100000000using namespace std;struct Edge//求解最小割 {int from, to, cap, flow, next;}edge[MAXM];int head[MAXN], edgenum;struct IT//求解最短路 {int from, to, val, next;}it[MAXM];int HH[MAXN], HHnum;int cur[MAXN];int dist[MAXN];bool vis[MAXN];int rec[MAXN];//rec[i]记录最少经过几条边 可到达i  用来求第二个答案 int N, M;void initHH(){HHnum = 0;memset(HH, -1, sizeof(HH));}void initEdge(){edgenum = 0;memset(head, -1, sizeof(head));}void addIT(int u, int v, int w){IT E1 = {u, v, w, HH[u]};it[HHnum] = E1;HH[u] = HHnum++;IT E2 = {v, u, w, HH[v]};it[HHnum] = E2;HH[v] = HHnum++;}void addEdge(int u, int v, int w){Edge E1 = {u, v, w, 0, head[u]};edge[edgenum] = E1;head[u] = edgenum++;Edge E2 = {v, u, 0, 0, head[v]};edge[edgenum] = E2;head[v] = edgenum++;}void input(){int a, b, c;for(int i = 0; i < M; i++){scanf("%d%d%d", &a, &b, &c);addIT(a, b, c); }}void SPFA()//求出最短路径 + 到达N最少经过的边数 {queue<int> Q;for(int i = 1; i <= N; i++){dist[i] = i==1 ? 0 :INF;vis[i] = i==1 ? true : false;rec[i] = 0; }Q.push(1);while(!Q.empty()){int u = Q.front();Q.pop();vis[u] = false;for(int i = HH[u]; i != -1; i = it[i].next){IT E = it[i];if(dist[E.to] > dist[u] + E.val){dist[E.to] = dist[u] + E.val;rec[E.to] = rec[u] + 1;//更新最少边数 if(!vis[E.to]){vis[E.to] = true;Q.push(E.to);} }else if(dist[E.to] == dist[u] + E.val)rec[E.to] = min(rec[u] + 1, rec[E.to]);//更新最少边数 }}}//建新图 void getMap(){for(int u = 1; u <= N; u++)//枚举起点 {for(int j = HH[u]; j != -1; j = it[j].next){IT E = it[j]; if(dist[E.to] == dist[u] + E.val)//加入最短路上的边 //printf("%d -> %d  ", u, E.to), addEdge(u, E.to, 1);//权值为1 }}}//dinic求最小割 = 最大流 bool BFS(int start, int end){queue<int> Q;memset(dist, -1, sizeof(dist));memset(vis, false, sizeof(vis));dist[start] = 0;vis[start] = true;Q.push(start);while(!Q.empty()){int u = Q.front();Q.pop();for(int i = head[u]; i != -1; i = edge[i].next){Edge E = edge[i];if(!vis[E.to] && E.cap - E.flow > 0)//为标记 且 没有满流 {vis[E.to] = true;dist[E.to] = dist[u] + 1;//建立层次图 if(E.to == end) return true;Q.push(E.to); }}}return false;//找不到一条可以从源点到汇点的路径}int DFS(int x, int a, int end){if(x == end || a == 0) return a;//优化 int flow = 0, f;for(int &i = cur[x]; i != -1; i = edge[i].next){Edge &E = edge[i];if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(E.cap - E.flow, a), end)) > 0){E.flow += f;edge[i^1].flow -= f;flow += f;a -= f;if(a == 0) break;}}return flow;}int Maxflow(int start, int end){int flow = 0;while(BFS(start, end))//找到增广路 {memcpy(cur, head, sizeof(head));flow += DFS(start, INF, end);}return flow;}int main(){while(scanf("%d%d", &N, &M) != EOF){initHH();input();SPFA();//先求最短路 + 到达N经过的最少边数 initEdge();//初始化 getMap();//建新图//for(int i = 1; i <= N; i++)//{//for(int j = head[i]; j != -1; j = edge[j].next)//printf("%d -> %d %d ..", i, edge[j].to, edge[j].cap);//printf("\n");//}        //printf("%d\n", rec[N]);printf("%d %d\n", Maxflow(1, N), M - rec[N]); } return 0;}



0 0
原创粉丝点击