博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Friendship POJ - 1815 基本建图
阅读量:5088 次
发布时间:2019-06-13

本文共 5614 字,大约阅读时间需要 18 分钟。

In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone. You can assume that people A can keep in touch with people B, only if 

1. A knows B’s phone number, or 
2. A knows people C’s phone number and C can keep in touch with B. 
It’s assured that if people A knows people B’s number, B will also know A’s number.

Sometimes, someone may meet something bad which makes him lose touch with all the others. For example, he may lose his phone number book and change his phone number at the same time.

In this problem, you will know the relations between every two among N people. To make it easy, we number these N people by 1,2,…,N. Given two special people with the number S and T, when some people meet bad things, S may lose touch with T. Your job is to compute the minimal number of people that can make this situation happen. It is supposed that bad thing will never happen on S or T. 

Input 
The first line of the input contains three integers N (2<=N<=200), S and T ( 1 <= S, T <= N , and S is not equal to T).Each of the following N lines contains N integers. If i knows j’s number, then the j-th number in the (i+1)-th line will be 1, otherwise the number will be 0.

You can assume that the number of 1s will not exceed 5000 in the input. 

Output 
If there is no way to make A lose touch with B, print “NO ANSWER!” in a single line. Otherwise, the first line contains a single number t, which is the minimal number you have got, and if t is not zero, the second line is needed, which contains t integers in ascending order that indicate the number of people who meet bad things. The integers are separated by a single space.

If there is more than one solution, we give every solution a score, and output the solution with the minimal score. We can compute the score of a solution in the following way: assume a solution is A1, A2, …, At (1 <= A1 < A2 <…< At <=N ), the score will be (A1-1)*N^t+(A2-1)*N^(t-1)+…+(At-1)*N. The input will assure that there won’t be two solutions with the minimal score.

 

题目大意:目标人物T和S只要能间接联络到对方就算可以联络,如果T通过A和S联系,如果A换号码了,就切断了与T和S的联系,T和S就无法联系了,现在让我们找出最小要几个人(T和S不能发生事故)发生事故可以使S和T切断联系。

简单来说就是切断网络流导致源点到汇点的流量为0就行,不过这题要切断的是点,所以需要把一个点拆成两个点,分别为x和x+n,x到x+n间连一条容量为1的边,然后所有入边连到x,出边从x+n发出,这样一来只要切断x到x+n间的那条边,这个点就不会有任何流量流过。初次求最大流的最大流量就是有几条通路可以使S和T保持联络,我们依次枚举所有点i拆成的i和i+n之间的边,将其容量修改为0,然后重新计算最大流(要清除边的flow),如果最大流减小了,则这个点切断会影响联络。

 以上内容来自   https://blog.csdn.net/hsj970319/article/details/62416798  

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define pi acos(-1.0)#define eps 1e-6#define fi first#define se second#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define bug printf("******")#define mem(a,b) memset(a,b,sizeof(a))#define fuck(x) cout<<"["<
<<"]"<
= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define FIN freopen("in.txt","r",stdin)#define lowbit(x) x&-x#pragma comment (linker,"/STACK:102400000,102400000")using namespace std;const int maxn = 200004;typedef long long LL;const int MX = 10005;const int MXE = 4 * MX * MX;const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;const int INF = 0x3f3f3f;int tot, num, s, t;int head[MX];struct Edge { int v, nxt; LL w;} edge[200010];void init() { memset (head, -1, sizeof (head) ); tot = 0;}void add (int u, int v, LL w) { edge[tot].v = v; edge[tot].w = w; edge[tot].nxt = head[u]; head[u] = tot++; edge[tot].v = u; edge[tot].w = 0; edge[tot].nxt = head[v]; head[v] = tot++;}int d[MX], vis[MX], gap[MX];void bfs() { memset (d, 0, sizeof (d) ); memset (gap, 0, sizeof (gap) ); memset (vis, 0, sizeof (vis) ); queue
q; q.push (t); vis[t] = 1; while (!q.empty() ) { int u = q.front(); q.pop(); for (int i = head[u]; ~i; i = edge[i].nxt) { int v = edge[i].v; if (!vis[v]) { d[v] = d[u] + 1; gap[d[v]]++; q.push (v); vis[v] = 1; } } }}int last[MX];LL dfs (int u, LL f) { if (u == t) return f; LL sap = 0; for (int i = last[u]; ~i; i = edge[i].nxt) { int v = edge[i].v; if (edge[i].w > 0 && d[u] == d[v] + 1) { last[u] = i; LL tmp = dfs (v, min (f - sap, edge[i].w) ); edge[i].w -= tmp; edge[i ^ 1].w += tmp; sap += tmp; if (sap == f) return sap; } } if (d[s] >= num) return sap; if (! (--gap[d[u]]) ) d[s] = num; ++gap[++d[u]]; last[u] = head[u]; return sap;}LL solve (int st, int ed, int n) { LL flow = 0; num = n; s = st; t = ed; bfs(); memcpy (last, head, sizeof (head) ); while (d[s] < num) flow += dfs (s, INFLL); return flow;}int n, sp, tp, x, cnt[505], mp[505][505];void build() { init(); for (int i = 1 ; i <= n ; i++) { if (!cnt[i]) add(i, i + n, 1); for (int j = 1 ; j <= n ; j++) { if (mp[i][j] && i != j ) add(i + n, j, INF); } }}int main() { while(~scanf("%d%d%d", &n, &sp, &tp)) { int flag = 0; memset(cnt, 0, sizeof(cnt)); for (int i = 1 ; i <= n ; i++) add(i, i + n, 1); for (int i = 1 ; i <= n ; i++) { for (int j = 1 ; j <= n ; j++) { scanf("%d", &mp[i][j]); if (mp[i][j] && ((i == sp && j == tp) || (i == tp && j == sp)))flag = 1; } } if (flag) { printf("NO ANSWER!\n"); continue; } build(); int temp = (int)(solve(sp + n, tp, 3* n)); printf("%d\n", temp); vector
path; path.clear(); for (int i = 1 ; i <= n ; i++) { if (i == sp || i == tp ) continue; cnt[i] = 1; build(); if (temp > (int)(solve(sp + n, tp, 2 * n))) temp--, path.push_back(i); else cnt[i] = 0; if (temp <= 0) break; } for (int i = 0 ; i < path.size() ; i++) printf("%d%c", path[i], i == path.size() - 1 ? '\n' : ' '); } return 0;}

 

转载于:https://www.cnblogs.com/qldabiaoge/p/9415824.html

你可能感兴趣的文章
Django组件——分页器和中间件
查看>>
scala 14 trait
查看>>
You need to run build with JDK or have tools.jar问题解决
查看>>
BZOJ 1030: [JSOI2007]文本生成器 [AC自动机 DP]
查看>>
HDU 3949 XOR [高斯消元XOR 线性基]
查看>>
LeetCode Best Time to Buy and Sell Stock III
查看>>
PHP变量引用赋值后unset 输出原始变量,值存在.
查看>>
简单的新浪微博爬虫-Python版-(下载部分)---(上)
查看>>
for-each用法误区(不能改变数组元素值)
查看>>
f.select
查看>>
SSH2各部分作用
查看>>
不设置默认网关,导致traceroute无法获取途经路由信息原因
查看>>
MySql优化—删除操作
查看>>
三天打渔两天晒网
查看>>
python编码的那些事
查看>>
5.MVC框架开发(强类型开发,控制器向界面传递数据的几种方法)
查看>>
编程语言分类
查看>>
[转]GIT PUSH Error 403的解决方法
查看>>
Unity 移动主角的时候,鼠标被固定在屏幕中心而且被隐藏
查看>>
自已接触过的数据访问方式总结
查看>>