ZOJ 2587--Unique Attack【判断最小割是否唯一】
2015-09-22 22:08
621 查看
Unique Attack
Time Limit: 5 Seconds
Memory Limit: 32768 KB
N supercomputers in the United States of Antarctica are connected into a network. A network has asimple topology: M different pairs of supercomputers are connected to each other by an optical fibre.All connections are two-way, that is, they can be used in
both directions. Data can be transmitted fromone computer to another either directly by a fibre, or using some intermediate computers.
A group of terrorists is planning to attack the network. Their goal is to separate two main computersof the network, so that there is no way to transmit data from one of them to another. For each fibrethe terrorists have calculated the sum of money they
need to destroy the fibre. Of course, they want tominimize the cost of the operation, so it is required that the total sum spent for destroying the fibres wasminimal 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 fibre connections that can be destroyed, such thatthe main supercomputers cannot connect to each
other after it and the cost of the operation is minimalpossible.
Input
The input file consists of several cases. In each case,the first line of the input file contains N, M, A and B (2 <= N <= 800, 1 <= M <= 10000, 1 <= A,B <= N,A != B), specifying the number of supercomputers in the network, the number of fibre connections,
andthe numbers of the main supercomputers respectively. A case with 4 zeros indicates the end of file.
Next M lines describe fibre connections. For each connection the numbers of the computers it connectsare given and the cost of destroying this connection. It is guaranteed that all costs are non-negativeinteger numbers not exceeding 105, no two computers
are directly connected by more than one fibre,no fibre connects a computer to itself and initially there is the way to transmit data from one mainsupercomputer to another.
Output
If there is only one way to perform the operation, output "UNIQUE" in a single line.In the other case output "AMBIGUOUS".
Sample Input
Sample Output
题意:
给你N个点、M条无向边以及边的权值,又给你源点A和汇点B。问你A到B的最小割是否唯一
解析:
1、我们先对原图求一次最大流
2、对残留网络,我们从S开始,找到所有所有S能到达的点;再从T开始,找出所有能到达T的点。
3、判断原网络中是否还有没有访问到的点,如果没有,则唯一,否者,不唯一!
Time Limit: 5 Seconds
Memory Limit: 32768 KB
N supercomputers in the United States of Antarctica are connected into a network. A network has asimple topology: M different pairs of supercomputers are connected to each other by an optical fibre.All connections are two-way, that is, they can be used in
both directions. Data can be transmitted fromone computer to another either directly by a fibre, or using some intermediate computers.
A group of terrorists is planning to attack the network. Their goal is to separate two main computersof the network, so that there is no way to transmit data from one of them to another. For each fibrethe terrorists have calculated the sum of money they
need to destroy the fibre. Of course, they want tominimize the cost of the operation, so it is required that the total sum spent for destroying the fibres wasminimal 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 fibre connections that can be destroyed, such thatthe main supercomputers cannot connect to each
other after it and the cost of the operation is minimalpossible.
Input
The input file consists of several cases. In each case,the first line of the input file contains N, M, A and B (2 <= N <= 800, 1 <= M <= 10000, 1 <= A,B <= N,A != B), specifying the number of supercomputers in the network, the number of fibre connections,
andthe numbers of the main supercomputers respectively. A case with 4 zeros indicates the end of file.
Next M lines describe fibre connections. For each connection the numbers of the computers it connectsare given and the cost of destroying this connection. It is guaranteed that all costs are non-negativeinteger numbers not exceeding 105, no two computers
are directly connected by more than one fibre,no fibre connects a computer to itself and initially there is the way to transmit data from one mainsupercomputer to another.
Output
If there is only one way to perform the operation, output "UNIQUE" in a single line.In the other case output "AMBIGUOUS".
Sample Input
4 4 1 2 1 2 1 2 4 2 1 3 2 3 4 1 4 4 1 2 1 2 1 2 4 1 1 3 2 3 4 1 0 0 0 0
Sample Output
UNIQUE AMBIGUOUS
题意:
给你N个点、M条无向边以及边的权值,又给你源点A和汇点B。问你A到B的最小割是否唯一
解析:
1、我们先对原图求一次最大流
2、对残留网络,我们从S开始,找到所有所有S能到达的点;再从T开始,找出所有能到达T的点。
3、判断原网络中是否还有没有访问到的点,如果没有,则唯一,否者,不唯一!
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; #define maxn 1100 #define maxm 1100000 #define INF 0x3f3f3f3f int n, m, s, t; int head[maxn], cur[maxn], cnt; int dist[maxn], vis[maxn]; int num; struct node { int u, v, cap, flow, next; }; node edge[maxm]; void init(){ cnt = 0; memset(head, -1, sizeof(head)); } void add(int u, int v, int w){ edge[cnt] = {u, v, w, 0, head[u]}; head[u] = cnt++; edge[cnt] = {v, u, 0, 0, head[v]}; head[v] = cnt++; } void getmap(){ int a, b, c; while(m--){ scanf("%d%d%d", &a, &b, &c); add(a, b, c); add(b, a, c); } } bool BFS(int st ,int ed){ queue <int> q; memset(vis, 0, sizeof(vis)); memset(dist, -1, sizeof(dist)); dist[st] = 0; vis[st] = 1; q.push(st); while(!q.empty()){ int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(!vis[E.v] && E.cap > E.flow){ vis[E.v] = 1; dist[E.v] = dist[u] + 1; if(E.v == ed) return true; q.push(E.v); } } } return false; } int DFS(int x, int ed, int a){ if(x == ed || a == 0) return a; int flow = 0, f; for(int &i = cur[x]; i != -1; i = edge[i].next){ node &E = edge[i]; if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){ E.flow += f; edge[i ^ 1].flow -= f; a -= f; flow += f; if(a == 0) break; } } return flow; } void maxflow(int st, int ed){ while(BFS(st, ed)){ memcpy(cur, head, sizeof(head)); DFS(st, ed, INF); } } void dfs1(int u){//从S开始,找到所有所有S能到达的点 vis[u] = 1; for(int i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(!vis[E.v] && E.cap > E.flow) dfs1(E.v); } } void dfs2(int u){//从T开始,找出所有能到达T的点 vis[u] = 1; for(int i = head[u]; i != -1; i = edge[i].next){ node E = edge[i]; if(!vis[E.v] && edge[i ^ 1].cap > edge[i ^ 1].flow) dfs2(E.v); } } int main (){ while(scanf("%d%d%d%d", &n, &m, &s, &t), n || m || s || t){ init(); getmap(); maxflow(s, t); int flag = 0; memset(vis, 0, sizeof(vis)); vis[s] = vis[t] = 1; num = 0; dfs1(s); dfs2(t); for(int i = 1; i <= n; ++i) if(!vis[i]){ flag = 1; break; } if(flag == 1) printf("AMBIGUOUS\n"); else printf("UNIQUE\n");//不唯一 } return 0; }
相关文章推荐
- UIColor的使用及拓展
- BindingExpression path using IMultiValueConverter in datacolumn
- JAVA问题总结之24--HashMap键值对(key-value)的操作
- iOS开发之UIActivityIndicatorView(二)
- UIButton的响应事件 UIControlEvents 类型说明
- Unix环境编程学习笔记-------通读APUE第一遍后的疑问???????
- systemUI之statusbar
- IOS学习之 UITabBar
- UIday2201:IOS开发中的多线程
- 判断修改的中的值,用前面的,否则容易获得空值;this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue; VS bool b = (bool)this.dataGridView1.Rows[i].Cells[0].Value;
- iOS开发之UIalertView
- UI一揽子计划 22 (多线程概述、NSThread、NSOperationQueue 、GCD、多线程管理)
- UI一揽子计划 21 (UICollectionView、SDWebImage第三方类库加载图片的使用、集合视图的布局UICollectionViewFlowLayout 、自定义Cell、布局协议
- iOS9新特性-UIStackView(简单介绍)
- iOS开发 ----- UISearchBar_UISearchController
- iOS开发 ----- UICollectionView
- iOS开发 ----- UITableView
- iOS开发 ----- UIScrollview UIPageControl
- iOS通过UIAlertView实现Android Toast效果
- iOS开发 ----- UIGestureRecognizer