POJ 1815 Friendship(字典序最小的最小割)
| Time Limit: 2000MS | Memory Limit: 20000K | |
| Total Submissions: 10744 | Accepted: 2984 |
Description
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
You can assume that the number of 1s will not exceed 5000 in the input.
Output
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 3
1 1 0
1 1 1
0 1 1
Sample Output
1
2
题目链接:POJ 1815
给了一个用邻接矩阵表示的无向图,断开S与T点的最少点数集且这个集合不能包含S与T,若这个集合不为0,则输出字典序最小的一种方案。
题意显然是求最少割点集,肯定要拆点了, 考虑原图一个人的影响,去掉这个人则与与他直接连接的人均无法连接到他,因此自身拆出来的边流量为1,为了保证S与T不在集合中,这两个点的边流量为INF,然后顺序枚举各个点,若去掉当前点流量变小了当前边权的值,则说明这个点就在割边集中。
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
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<int, int> 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<int>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<int>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;
}
POJ 1815 Friendship(字典序最小的最小割)的更多相关文章
- poj 1815 Friendship 字典序最小+最小割
题目链接:http://poj.org/problem?id=1815 In modern society, each person has his own friends. Since all th ...
- POJ 1815 Friendship ★(字典序最小点割集)
[题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...
- POJ 1815 Friendship(最小割+字典序输出割点)
http://poj.org/problem?id=1815 题意: 在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A可以和B保持联系,当且仅当:①A知道B的电话号 ...
- POJ 1815 Friendship(最小割)
http://poj.org/problem? id=1815 Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissio ...
- POJ 1815 Friendship (Dinic 最小割)
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 8025 Accepted: 2224 Descri ...
- poj 1815 Friendship (最小割+拆点+枚举)
题意: 就在一个给定的无向图中至少应该去掉几个顶点才干使得s和t不联通. 算法: 假设s和t直接相连输出no answer. 把每一个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量) ...
- poj 1815 Friendship【最小割】
网络流的题总是出各种奇怪的错啊--没写过邻接表版的dinic,然后bfs扫到t点不直接return 1就会TTTTTLE-- 题目中的操作是"去掉人",很容易想到拆点,套路一般是( ...
- POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]
妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...
- POJ 1815 Friendship(最大流最小割の字典序割点集)
Description In modern society, each person has his own friends. Since all the people are very busy, ...
随机推荐
- 【BZOJ2002】[HNOI2010] 弹飞绵羊(大力分块)
点此看题面 大致题意: 有\(n\)个弹力装置,当到达第\(i\)个装置时,会被弹到第\(i+k_i\)个装置,若不存在第\(i+k_i\)个装置,就会被弹飞.有两种操作,一种操作是将\(k_x\)改 ...
- 2018.6.10 Oracle数据库常见的错误汇总
1.ClassNoFoundException 找不到注册驱动 可能原因:1>驱动名称不对 2>没有导入数据库驱动包 2.SQl 语句中可以使用任何有效的函数,函数操作的列,必须指定别名, ...
- 第五章 javascript编程可养成的好习惯
用户点击某个链接时弹出一个新窗口javascript使用window对象的open()方法来创建新的浏览器窗口,这个方法有三个参数:window.open(url,name,features)url: ...
- Matlab将多幅图片保存为mat
%% 储存某目录所有的图片 pt = 'd:\imgs\'; ext = '*.jpg'; dis = dir([pt ext]); nms = {dis.name}; for k = 1:lengt ...
- axiospost请求向后端提交数据
Axios向后端提交数据容易接收不到原因是传参方式是request payload,参数格式是json,而并非用的是form传参,所以在后台用接收form数据的方式接收参数就接收不到了.post表单请 ...
- centos下安装mariadb
前言 一直都是前端,比较少玩服务器,最近买了阿里云,开始尝试centos,不得不说linux还是很有魅力的. 正文 google了一圈,决定安装mariadb,其实mariadb和mysql差不多,使 ...
- ABC108C - Triangular Relationship(打表)
题意 给出$n, k$,求出满足$a+b, b + c, c + a$都是$k$的倍数的三元组$a, b, c$的个数,$1 \leqslant a, b, c \leqslant N$ $n \le ...
- 自封装的AJAX
/* * *create by royal in 2019/1/23 * *royalAjax 自封装ajax函数 * *paramsObj JSON类型参数 *require params: * t ...
- JS:字符串转成json数据,和json转成字符串方法 iframe获取父级传过来的数据
字符串转成json数据,和json转成字符串方法 //转为JSON adinfo=JSON.parse(adinfo) //转为字符串 adinfo=JSON.stringify(adinfo) 大概 ...
- Foxmail登录不了网易企业邮箱解决办法
关于Foxmail登录不了网易企业邮箱问题 解决办法是:在设置账号的时候手动设置pop服务器和smtp服务器. 新建账号的图: 点击“手动设置”出现如下界面: 设置完成后问题解决.下面的两个是正确的, ...