A-unique attack
Time limit:6000/3000 ms (Java/Others)
Memory limit:128000/64000 KB (Java/others) Problem Description
N supercomputers in the United States of Antarctica are connected into a network. A network has a simple topology: m different pairs of supercomputers are connected to each other by an optical fiber. all connections are two-way, that is, they can be used in both directions ctions. data can be transmitted from one computer to another either directly by a fiber, or using some intermediate computers.
A group of terrorists is planning to attack the network. their goal is to separate two main computers of the network, so that there is no way to transmit data from one of them to another. for each fiber the terrorists have calculated the sum of money they need to destroy the fiber. of course, they want to minimize the cost of the operation, so it is required that the total sum spent for destroying the fiber ers was minimal possible.
Now the leaders of the group wonder whether there is only one way to do the selected operation. that is, they want to know if there are no two different sets of fiber connections that can be destroyed, such that the main supercomputers cannot connect to each other after it and the cost of the operation is minimal possible.
Input
The first line of the input file contains n, m, A and B (2 <= n <= 800, 1 <= m <= 10000, 1 <=, B <= n,! = B), specifying the number of supercomputers in the network, the number of fiber connections, and the numbers of the main supercomputers respectively. A case with 4 zeros indicates the end of file.
Next m lines describe fiber connections. for each connection the numbers of the computers it connects are given and the cost of destroying this connection. it is guaranteed that all costs are non-negative integer numbers not exceeding 105, no two computers are directly connected by more than one fiber, no fiber connects a computer to itself and initially there is the way to transmit data from one main supercomputer to another.
Output if there is only one way to perform the operation, output "unique". In the other case output "ambiguous". sample input
4 4 1 21 2 12 4 21 3 23 4 14 4 1 21 2 12 4 11 3 23 4 1
Sample output
Uniqueambiguous
Problem solving: uniqueness determination of the minimum cut
Using the residual amount network, points except the source sink point can either be reached from the source point along the arc of the not full stream or to the big sink. It is unique. If a vertex exists, neither the Source Vertex nor the sink vertex can be reached, it is not unique.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 using namespace std; 16 const int maxn = 810; 17 struct arc { 18 int to,flow,next; 19 arc(int x = 0,int y = 0,int z = -1) { 20 to = x; 21 flow = y; 22 next = z; 23 } 24 }; 25 arc e[maxn*maxn]; 26 int head[maxn],d[maxn],cur[maxn]; 27 int n,m,S,T,tot,src,sink; 28 void add(int u,int v,int flow) { 29 e[tot] = arc(v,flow,head[u]); 30 head[u] = tot++; 31 e[tot] = arc(u,0,head[v]); 32 head[v] = tot++; 33 } 34 bool bfs() { 35 queue<int>q; 36 memset(d,-1,sizeof(d)); 37 d[S] = 1; 38 q.push(S); 39 while(!q.empty()) { 40 int u = q.front(); 41 q.pop(); 42 for(int i = head[u]; ~i; i = e[i].next) { 43 if(e[i].flow && d[e[i].to] == -1) { 44 d[e[i].to] = d[u] + 1; 45 q.push(e[i].to); 46 } 47 } 48 } 49 return d[T] > -1; 50 } 51 int dfs(int u,int low) { 52 if(u == T) return low; 53 int tmp = 0,a; 54 for(int &i = cur[u]; ~i; i = e[i].next) { 55 if(e[i].flow && d[e[i].to] == d[u] + 1 && (a=dfs(e[i].to,min(low,e[i].flow)))) { 56 tmp += a; 57 low -= a; 58 e[i].flow -= a; 59 e[i^1].flow += a; 60 if(!low) break; 61 } 62 } 63 if(tmp == 0) d[u] = -1; 64 return tmp; 65 } 66 int dinic() { 67 int tmp = 0; 68 while(bfs()) { 69 memcpy(cur,head,sizeof(head)); 70 tmp += dfs(S,INF); 71 } 72 return tmp; 73 } 74 bool vis[maxn]; 75 void dfs1(int u) { 76 for(int i = head[u]; ~i; i = e[i].next) { 77 if(!vis[e[i].to] && e[i].flow) { 78 vis[e[i].to] = true; 79 dfs1(e[i].to); 80 } 81 } 82 } 83 void dfs2(int u) { 84 for(int i = head[u]; ~i; i = e[i].next) { 85 if(!vis[e[i].to] && e[i^1].flow) { 86 vis[e[i].to] = true; 87 dfs2(e[i].to); 88 } 89 } 90 } 91 int main() { 92 int u,v,w; 93 while(~scanf("%d %d %d %d",&n,&m,&S,&T)) { 94 memset(head,-1,sizeof(head)); 95 for(int i = tot = 0; i < m; i++) { 96 scanf("%d %d %d",&u,&v,&w); 97 add(u,v,w); 98 add(v,u,w); 99 }100 dinic();101 //cout<<dinic()<<endl;102 memset(vis,false,sizeof(vis));103 vis[S] = vis[T] = true;104 dfs1(S);105 dfs2(T);106 bool flag = true;107 for(int i = 1; i <= n; i++)108 if(!vis[i]) {109 flag = false;110 break;111 }112 flag?puts("UNIQUE"):puts("AMBIGUOUS");113 }114 return 0;115 }
View code
Acdream A-unique attack