poj 1815 Friendship 字典序最小+最小割
题目链接:http://poj.org/problem?id=1815
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.
题目描述:n个人,给出一些关系(两个人之间直接联系或可以间接联系),现在破坏一些人,使S和T这两个人不能联系。求出最小的人数(输出字典序最小的方案)。
算法分析:最小割解之,这个不用说了。重点在于怎么求解字典序最小:由于节点较少,我们可以一一枚举节点u,然后去掉u->u'后求解最小割是否会使最小割变小,是则必须删掉此边。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=,M=; int n,from,to;
struct node
{
int v,flow;
int next;
} edge[M*],save[M*];
int head[maxn],edgenum; void add(int u,int v,int flow)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow ;
edge[edgenum].next=head[u] ;head[u]=edgenum++; edge[edgenum].v=u ;edge[edgenum].flow=;
edge[edgenum].next=head[v] ;head[v]=edgenum++;
} int d[maxn];
int bfs()
{
memset(d,,sizeof(d));
d[from]=;
queue<int> Q;
Q.push(from);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (!d[v] && edge[i].flow)
{
d[v]=d[u]+;
Q.push(v);
if (v==to) return ;
}
}
}
return ;
} int dfs(int u,int flow)
{
if (u==to || flow==) return flow;
int cap=flow;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (d[v]==d[u]+ && edge[i].flow)
{
int x=dfs(v,min(cap,edge[i].flow));
edge[i].flow -= x;
edge[i^].flow += x;
cap -= x;
if (cap==) return flow;
}
}
return flow-cap;
} int dinic()
{
int ans=;
while (bfs()) ans += dfs(from,inf);
return ans;
} int an[maxn][maxn];
int main()
{
while(scanf("%d%d%d",&n,&from,&to)!=EOF)
{
bool flag=false;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&an[i][j]);
}
}
if(an[from][to])
{
printf("NO ANSWER!\n");
continue;
}
memset(head,-,sizeof(head));
edgenum=;
for (int i= ;i<=n ;i++)
{
if (i!=from && i!=to)
add(i,i+n,);
for (int j= ;j<=n ;j++)
{
if (an[i][j]&&i!=j)
{
if (i==from)
add(i,j,inf);
else if (i!=to)
add(i+n,j,inf);
}
}
}
memcpy(save,edge,sizeof(node)*edgenum);
int ans=dinic();
printf("%d\n",ans);
for (int i= ;i<=n ;i++)
{
if (i!=from && i!=to)
for (int j=head[i] ;j!=- ;j=edge[j].next)
{
if (edge[j].flow== && edge[j].v==i+n)
{
save[j].flow=save[j^].flow=;
memcpy(edge,save,sizeof(node)*edgenum);
if (dinic()!=ans-)
{
save[j].flow=;
save[j^].flow=;
continue;
}
if (flag)
printf(" ");
else
flag=true;
printf("%d",i);
ans--;
break;
}
}
}
printf("\n");
}
return ;
}
poj 1815 Friendship 字典序最小+最小割的更多相关文章
- POJ 1815 Friendship (Dinic 最小割)
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 8025 Accepted: 2224 Descri ...
- POJ 1815 Friendship ★(字典序最小点割集)
[题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...
- POJ 1815 Friendship(字典序最小的最小割)
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 10744 Accepted: 2984 Descr ...
- POJ 1815 Friendship(最小割)
http://poj.org/problem? id=1815 Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissio ...
- POJ 1815 Friendship(最小割+字典序输出割点)
http://poj.org/problem?id=1815 题意: 在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A可以和B保持联系,当且仅当:①A知道B的电话号 ...
- 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, ...
随机推荐
- 7)Java数据类型
Java中的数据类型分为基本数据类型和引用数据类型: 1)基础数据类型有: boolean, byte.short.char, int.float.long, dou ...
- 在GridControl控件中使用SearchLookUpEdit构建数据快速输入
较早之前,曾经介绍了一篇文章<使用DataGridView数据窗口控件,构建用户快速输入体验>,介绍了在传统DataGridView中嵌入一个数据窗口进行选择列表,从而实现数据快速录入的操 ...
- Javascript 插件统一的实现步骤
步骤: // 1. 定义立即调用的函数 +function($){ "use strict"; //使用严格模式ES5支持 //后续步骤 // 2. xx 插件类及原型方法的定义 ...
- Java 第五天 Spring IOC 配置文件
Spring xml结构定义文件: http://www.springframework.org/schema/beans/spring-beans.xsd 可用xsd列表: http://www.s ...
- .NET开源工作流RoadFlow-流程设计-保存与发布
如果流程未设计完时可以先保存,以后再打开接着设计.点击工具栏上的保存按钮即可保存当前流程设计: 如果下次要接着设计,则可以打开该流程继续设计: 如果流程设计完成,可以点击安装按钮来发布流程,流程安装成 ...
- EChart 关于图标控件的简单实用
1.下载前段框架并放入项目中去. 2.在js中调用 <!DOCTYPE html> <html lang="en"> <head> <me ...
- 使用IC框架开发跨平台App的备忘录123
1,关于图标与启动屏幕 icon.png 192x192splash.png 2208x2208 将这两个图片放在resources目录下,在终端执行:ionic resources --iocn - ...
- CPU 材料学才是最顶级的学科
cpu的物理组成3部分:逻辑部件.寄存器.控制部件 CPU具有以下4个方面的基本功能:数据通信,资源共享,分布式处理,提供系统可靠性 cpu处理4过程:提取.解码.执行.写回 http://baike ...
- mongoDB 3.0 安全权限访问控制
MongoDB3.0权限,啥都不说了,谷歌百度出来的全是错的.先安装好盲沟,简单的没法说. 首先,不使用 —auth 参数,启动 mongoDB: mongodb-linux-i686-3.0.0/b ...
- python pandas/numpy
import pandas as pdpd.merge(dataframe1,dataframe2,on='common_field',how='outer') replace NaN datafra ...