博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1815 Friendship(字典序最小的最小割)
阅读量:5038 次
发布时间:2019-06-12

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

Friendship
Time Limit: 2000MS   Memory Limit: 20000K
Total Submissions: 10744   Accepted: 2984

Description

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. 

Sample Input

3 1 31 1 01 1 10 1 1

Sample Output

12

 

 

题目链接:

给了一个用邻接矩阵表示的无向图,断开S与T点的最少点数集且这个集合不能包含S与T,若这个集合不为0,则输出字典序最小的一种方案。

题意显然是求最少割点集,肯定要拆点了, 考虑原图一个人的影响,去掉这个人则与与他直接连接的人均无法连接到他,因此自身拆出来的边流量为1,为了保证S与T不在集合中,这两个点的边流量为INF,然后顺序枚举各个点,若去掉当前点流量变小了当前边权的值,则说明这个点就在割边集中。

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define INF 0x3f3f3f3f#define LC(x) (x<<1)#define RC(x) ((x<<1)+1)#define MID(x,y) ((x+y)>>1)#define CLR(arr,val) memset(arr,val,sizeof(arr))#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);typedef pair
pii;typedef long long LL;const double PI = acos(-1.0);const int N = 210;struct edge{ int to, nxt, cap; edge() {} edge(int _to, int _nxt, int _cap): to(_to), nxt(_nxt), cap(_cap) {}} E[(N * (N >> 1) + N) << 2];int G[N][N];int head[N << 1], tot;int d[N << 1];bool del[N];void init(){ CLR(head, -1); tot = 0; CLR(del, false);}void resetG(){ CLR(head, -1); tot = 0;}inline void add(int s, int t, int c){ E[tot] = edge(t, head[s], c); head[s] = tot++; E[tot] = edge(s, head[t], 0); head[t] = tot++;}int bfs(int s, int t){ CLR(d, -1); d[s] = 0; queue
Q; Q.push(s); while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = head[u]; ~i; i = E[i].nxt) { int v = E[i].to; if (d[v] == -1 && E[i].cap > 0) { d[v] = d[u] + 1; if (v == t) return 1; Q.push(v); } } } return ~d[t];}int dfs(int s, int t, int f){ if (s == t || !f) return f; int ret = 0; for (int i = head[s]; ~i; i = E[i].nxt) { int v = E[i].to; if (d[v] == d[s] + 1 && E[i].cap > 0) { int df = dfs(v, t, min(f, E[i].cap)); if (df > 0) { E[i].cap -= df; E[i ^ 1].cap += df; ret += df; if (!(f -= df)) break; } } } if (!ret) d[s] = -2; return ret;}int dinic(int s, int t){ int ret = 0; while (bfs(s, t)) ret += dfs(s, t, INF); return ret;}int main(void){ int n, S, T, i, j; while (~scanf("%d%d%d", &n, &S, &T)) { init(); for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) scanf("%d", &G[i][j]); } if (G[S][T]) puts("NO ANSWER!"); else { vector
vec; int mf = 0; for (int pos = 0; pos <= n; ++pos) { if (pos == S || pos == T) continue; del[pos] = true; resetG(); for (j = 1; j <= n; ++j) { if (!del[j]) { if (j == S || j == T) { add(j, j + n, INF); //2n add(j + n, j, INF); } else { add(j, j + n, 1); add(j + n, j, 1); } } } for (i = 1; i <= n; ++i) //无向图只需用到上三角 { for (j = i + 1; j <= n; ++j) { if (G[i][j]) { add(i + n, j, INF); //2*n*n/2 add(j + n, i, INF); } } } int tf = dinic(S + n, T); if (!pos) mf = tf; else if (mf - tf == 1) { mf = tf; vec.push_back(pos); } else del[pos] = false; } int sz = vec.size(); printf("%d\n", sz); for (i = 0; i < sz; ++i) printf("%d%s", vec[i], i == sz - 1 ? "\n" : " "); } } return 0;}

转载于:https://www.cnblogs.com/Blackops/p/6573322.html

你可能感兴趣的文章
css_去掉默认样式
查看>>
TensorFlow2.0矩阵与向量的加减乘
查看>>
NOIP 2010题解
查看>>
javascript中的each遍历
查看>>
String中各方法多数情况下返回新的String对象
查看>>
浅谈tcp粘包问题
查看>>
UVA11524构造系数数组+高斯消元解异或方程组
查看>>
排序系列之——冒泡排序、插入排序、选择排序
查看>>
爬虫基础
查看>>
jquery.lazyload延迟加载图片第一屏问题
查看>>
HDU 1011 Starship Troopers (树形DP)
查看>>
手把手教你写DI_1_DI框架有什么?
查看>>
.net常见的一些面试题
查看>>
OGRE 源码编译方法
查看>>
上周热点回顾(10.20-10.26)
查看>>
C#正则表达式引发的CPU跑高问题以及解决方法
查看>>
云计算之路-阿里云上:“黑色30秒”走了,“黑色1秒”来了,真相也许大白了...
查看>>
APScheduler调度器
查看>>
设计模式——原型模式
查看>>
【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.1.CSS框架和其他功能
查看>>