zoukankan      html  css  js  c++  java
  • POJ 1815 Friendship


    Friendship
    Time Limit: 2000MS   Memory Limit: 20000K
    Total Submissions: 10222   Accepted: 2835

    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 3
    1 1 0
    1 1 1
    0 1 1
    

    Sample Output

    1
    2


    把每个顶点v变成网络流中的两个顶点,入口v1和出口v2,从v1到v2连一条容量为1的路,表示这个点可用于中转。这样把v1到v2的边切断就相当于删了v

    新建s为源点,t为汇点,分别代表要通讯的两个人

    然后就转化出了一个最小割网络流问题,用最小割最大流定理解题


    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    using namespace std;
    const int INF=0x6ffffff;
    struct NODE{
    	int w;//容量 
    	int f;//流量 
    }e[600][600];//邻接矩阵流量边 
    int n,s,t,S,T;
    int q[600],d[600];
    int aset[600];
    bool flag[600];
    void add_edge(int a,int b,int w){
    	e[a][b].w=w;
    	return;
    }
    int BFS(){
    	int head=1,tail=1;
    	memset(flag,0,sizeof(flag));
    	int u,v;
    	q[head]=t;
    	d[t]=0;flag[t]=1;
    	while(head<=tail){
    		u=q[head];
    		for(v=0;v<=t;v++)//从源点0开始 
    		{
    			if(!flag[v] && e[v][u].w>e[v][u].f){
    				d[v]=d[u]+1;
    				q[++tail]=v;
    				flag[v]=1;
    			}
    			if(flag[s])return 1;
    		}
    		head++;
    	}
    	return 0;
    }
    int DFS(int v,int low){
    	int i;
    	if(v==t)return low;
    	int flow;
    	for(i=0;i<=t;i++){
    		if(e[v][i].w>e[v][i].f && d[v]==d[i]+1){
    			if(flow=DFS(i,min(low,e[v][i].w-e[v][i].f))){
    				e[v][i].f+=flow;
    				e[i][v].f=-e[v][i].f;
    				return flow;
    			}
    		}
    	}
    	return 0;
    }
    void dinic(){
    	int ans=0;
    	while(BFS()){
    		int flow;
    		while(flow=DFS(s,INF)){
    			ans+=flow;
    		}
    	}
    	printf("%d
    ",ans);
    	if(!ans)return;//S和T本来就不是朋友,这真是一个悲伤的故事
    	int tmp=ans,cnt=0;  int i,k;
    	for(i=1;i<=n && tmp;i++){//从小到大枚举,保证解按题意最优 
    		if(i==S||i==T)continue;
    		if(!e[i][i+n].f)continue;//本来就流不到,则不用尝试断边 
    		e[i][i+n].w=0;
    		for(int a=1;a<=t;a++)
    		  for(int b=1;b<=t;b++)
    		    e[a][b].f=0;//流量初始化
    		k=0;
    		while(BFS()){
    			int flow;
    			while(flow=DFS(S,INF))k+=flow;
    		} 
    		if(k!=tmp){//断边后新流小于原流,则记录答案 
    			aset[++cnt]=i;
    			tmp=k;
    		}
    		else e[i][i+n].w=1;//否则恢复 
    	}
    	//print
    	for(i=1;i<ans;i++)printf("%d ",aset[i]);
    	printf("%d
    ",aset[ans]);
    	//
    }
    int main(){
    	while(scanf("%d%d%d",&n,&S,&T)!=EOF){
    		memset(e,0,sizeof(0));
    		int i,j,x;
    		s=0;t=2*n+1;
    		//网络流起止点初始化 
    		add_edge(s,S,INF);
    		add_edge(T+n,t,INF);
    		//
    		for(i=1;i<=n;i++){
    			add_edge(i,i+n,1);//加一条自己到自己镜像的边,容量为1
    			for(j=1;j<=n;j++){
    				scanf("%d",&x);
    				if(x)add_edge(i+n,j,INF);//i镜像连接到j
    				//这样通过断开i到i+n的边(此边容量为1)就可以断开联系 
    			} 	
    		}
    		add_edge(S,S+n,INF);
    		add_edge(T,T+n,INF);
    		if(!e[S+n][T].w) dinic();//读入邻接矩阵后如果发现S与T直接相连,那么无解 
    		else printf("NO ANSWER!
    ");
    	}
    	return 0;
    }








  • 相关阅读:
    Angular9 cdk-virtual-scroll-viewport' is not a known element 报错解决方案
    angular8打包时提示ERROR in Child compilation failed:解决方案
    angular8配置proxy本地跨域代理
    Vue-cli 本地跨域配置方法
    axios中url参数变量配置
    在vue-cli中使用axios时报错TypeError: Cannot set property 'lists' of undefined at eval
    nuxt.js element-ui踩坑记录(已解决)
    nuxt 关闭ESlint 语法检测
    javaScript学习之正则表达式初探
    直接在低版本IE6/7/8浏览器中使用HTML5的audio和video标签播放视频音频的办法
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5550580.html
Copyright © 2011-2022 走看看