ZOJ 2587 Unique Attack
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1587
题意:N 台超级计算机连成一个网络。M 对计算机之间用光纤直接连在一起,光纤的连接是双向的。数据可以直接在有光纤直接连接的计算机之间传输,也可以通过一些计算机作为中转来传输。
有一群恐怖分子计划攻击网络。他们的目标是将网络中两台主计算机断开,这样这两台计算机之间就无法传输数据了。恐怖分子已经计算好了摧毁每条光纤所需要花费的钱。当然了,他们希望攻击的费用最少,因此就必须使得需要摧毁的光纤费用总和最少。
现在,恐怖分子的头头想知道要达到目标且费用最少,是否只有一种方案。
思路:给定一个无向图,每条边上有权值。两个点,保证两个点之间连通。问最小割是否唯一。
从源点出发,走非满流的边,所有能够遍历到的点属于S集合。
从汇点出发,走非满流的边,所有能够遍历到的点属于T集合。(从汇点遍历必须重新构图)
首先可以保证,S集合和T集合内不会有相同的点,但两者的和并不一定是全集。因为有些点,到S集合为满流,到T集合也为满流,这些点既可以划分到S集合,也可以划分为T集合。所以当有这些点存在时,即S集合 + T集合不是全集时,最小割不唯一。
代码:
/* ID: [email protected] PROG: LANG: C++ */ #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<cstdio> #include<vector> #include<string> #include<fstream> #include<cstring> #include<ctype.h> #include<iostream> #include<algorithm> using namespace std; #define INF (1<<30) #define PI acos(-1.0) #define mem(a, b) memset(a, b, sizeof(a)) #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define eps 1e-6 #define debug puts("===============") #define pb push_back #define mkp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define POSIN(x,y) (0 <= (x) && (x) < n && 0 <= (y) && (y) < m) typedef long long ll; typedef unsigned long long ULL; const int maxn = 1000; const int maxm = 222222; int m; struct node { int u; int v; // vertex int cap; // capacity int flow; // current flow in this arc int nxt; } e[maxm * 2]; int g[maxn], cnt; int st, ed, n, N, M, A, B; void add(int u, int v, int c) { e[++cnt].v = v; e[cnt].u = u; e[cnt].cap = c; e[cnt].flow = 0; e[cnt].nxt = g[u]; g[u] = cnt; e[++cnt].v = u; e[cnt].u = v; e[cnt].cap = 0; e[cnt].flow = 0; e[cnt].nxt = g[v]; g[v] = cnt; } void init() { memset(g, 0, sizeof(g)); cnt = 1; int u, v, c; while(M--) { scanf("%d%d%d", &u, &v, &c); add(u, v, c); add(v, u, c); } st = A, ed = B; n = N + 3; } int dist[maxn], numbs[maxn], q[maxn]; void rev_bfs() { int font = 0, rear = 1; for (int i = 0; i <= n; i++) { //n为总点数 dist[i] = maxn; numbs[i] = 0; } q[font] = ed; dist[ed] = 0; numbs[0] = 1; while(font != rear) { int u = q[font++]; for (int i = g[u]; i; i = e[i].nxt) { if (e[i ^ 1].cap == 0 || dist[e[i].v] < maxn) continue; dist[e[i].v] = dist[u] + 1; ++numbs[dist[e[i].v]]; q[rear++] = e[i].v; } } } int maxflow() { rev_bfs(); int u, totalflow = 0; int curg[maxn], revpath[maxn]; for(int i = 0; i <= n; ++i) curg[i] = g[i]; u = st; while(dist[st] < n) { if(u == ed) { // find an augmenting path int augflow = INF; for(int i = st; i != ed; i = e[curg[i]].v) augflow = min(augflow, e[curg[i]].cap); for(int i = st; i != ed; i = e[curg[i]].v) { e[curg[i]].cap -= augflow; e[curg[i] ^ 1].cap += augflow; e[curg[i]].flow += augflow; e[curg[i] ^ 1].flow -= augflow; } totalflow += augflow; u = st; } int i; for(i = curg[u]; i; i = e[i].nxt) if(e[i].cap > 0 && dist[u] == dist[e[i].v] + 1) break; if(i) { // find an admissible arc, then Advance curg[u] = i; revpath[e[i].v] = i ^ 1; u = e[i].v; } else { // no admissible arc, then relabel this vertex if(0 == (--numbs[dist[u]])) break; // GAP cut, Important! curg[u] = g[u]; int mindist = n; for(int j = g[u]; j; j = e[j].nxt) if(e[j].cap > 0) mindist = min(mindist, dist[e[j].v]); dist[u] = mindist + 1; ++numbs[dist[u]]; if(u != st) u = e[revpath[u]].v; // Backtrack } } return totalflow; } int vis[maxn]; vector<int> G[maxn]; int stcnt, edcnt; void dfs1(int u) { stcnt++; vis[u] = 1; for (int i = g[u]; i; i = e[i].nxt) if (!vis[e[i].v] && e[i].cap) { dfs1(e[i].v); } } void dfs2(int u) { edcnt++; vis[u] = 1; for (int i = 0; i < G[u].size(); i++) if (!vis[G[u][i]]) dfs2(G[u][i]); } int main () { while(~scanf("%d%d%d%d", &N, &M, &A, &B), N || M || A || B) { init(); maxflow(); //先做一次网络流得到最小割 memset(vis, 0, sizeof(vis)); stcnt = edcnt = 0; for (int i = 0; i <= N; i++) G[i].clear(); for (int i = 2; i < cnt; i += 2) if (e[i].cap) { G[e[i].v].pb(e[i].u); //将非满流的边反向构图,以便从汇点dfs } dfs1(st); // 从源点dfs dfs2(ed); // 从汇点dfs if (stcnt + edcnt == N) puts("UNIQUE"); else puts("AMBIGUOUS"); } return 0; }
时间: 2024-10-10 13:43:15