zoukankan      html  css  js  c++  java
  • POJ 1815

    妖怪题目,做到现在:2017/8/19 - 1:41……

    不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√

    题目链接:http://poj.org/problem?id=1815

    Time Limit: 2000MS Memory Limit: 20000K

    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

    总的来说,就是求最小点割集,做法参考:

      http://www.cnblogs.com/lochan/p/3870697.html

      http://wugj03.blog.163.com/blog/static/1737650582011219115316710/

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<queue>
      4 #define in(x) x
      5 #define out(x) x+n
      6 #define MAX 500
      7 #define INF 0x3f3f3f3f
      8 using namespace std;
      9 struct Dinic{
     10     int s,t,nv;//源点、汇点、点总数 
     11     int c[MAX][MAX],f[MAX][MAX],lev[MAX];
     12     bool vis[MAX];
     13     void addedge(int from,int to,int cap)
     14     {
     15         c[from][to]=cap, f[from][to]=0;
     16         c[to][from]=0, f[to][from]=0;
     17     }
     18     bool bfs()
     19     {
     20         memset(vis,0,sizeof(vis));
     21         queue<int> q;
     22         q.push(s);
     23         vis[s]=1;
     24         lev[s]=0;
     25         while(!q.empty())
     26         {
     27             int u=q.front();q.pop();
     28             for(int v=1;v<=nv;v++)
     29             {
     30                 if(!vis[v] && c[u][v]>f[u][v])//属于残存网络的边 
     31                 {
     32                     lev[v]=lev[u]+1;
     33                     q.push(v);
     34                     vis[v]=1;
     35                 }
     36             }
     37             
     38         }
     39         return vis[t];
     40     }
     41     int dfs(int u,int aug)
     42     {
     43         if(u==t) return aug;
     44         int res=aug,tmp;
     45         for(int v=1;v<=nv;v++)
     46         {
     47             if(lev[v]==lev[u]+1 && c[u][v]>f[u][v])
     48             {
     49                 tmp=dfs(v,min(aug,c[u][v]-f[u][v]));
     50                 f[u][v]+=tmp;
     51                 f[v][u]-=tmp;
     52                 aug-=tmp;
     53             }
     54         }
     55         return res-aug;
     56     }
     57     int maxflow()
     58     {
     59         int res=0;
     60         while(bfs()) res+=dfs(s,INF);
     61         return res;
     62     }
     63 }dinic;
     64 
     65 int n,S,T;
     66 int main(){
     67     int a;
     68     scanf("%d%d%d",&n,&S,&T);
     69     dinic.nv=n*2, dinic.s=out(S), dinic.t=in(T);
     70     for(int i=1;i<=n;++i)
     71     {
     72         if(i!=dinic.s && i!=dinic.t) dinic.addedge(in(i),out(i),1);
     73         for(int j=1,tmp;j<=n;j++)
     74         {
     75             scanf("%d",&tmp);
     76             if(i!=j && tmp) dinic.addedge(out(i),in(j),INF);
     77         }
     78     }
     79     if(dinic.c[dinic.s][dinic.t]){
     80         puts("NO ANSWER!
    ");
     81         return 0;
     82     }
     83     int ans=dinic.maxflow();
     84     printf("%d
    ",ans);
     85     
     86     for(int i=1;i<=n && ans;i++)
     87     {
     88         if(i==dinic.s|| i==dinic.t || !dinic.f[in(i)][out(i)]) continue;
     89         memset(dinic.f,0,sizeof(dinic.f));
     90         dinic.c[in(i)][out(i)]=0;
     91         if(dinic.maxflow()<ans)
     92         {
     93             ans--;
     94             printf("%d ",i);
     95         }
     96         else dinic.c[in(i)][out(i)]=1;
     97     }
     98     printf("
    ");
     99     return 0;
    100 }

    PS.为了方便后续使用该模板,把它也封装在一个struct里了,1~63行为模板。

  • 相关阅读:
    重拾数学--初中--有理数
    Python中的运算符
    PyQt5实现虚拟摇杆
    Python无重复字符的最长子串
    Python两数相加
    Python两数之和
    DBMS,B树和B+树
    浮点数表示
    Lamada表达式
    Java编程思想P159页的错误
  • 原文地址:https://www.cnblogs.com/dilthey/p/7393012.html
Copyright © 2011-2022 走看看