zoukankan      html  css  js  c++  java
  • zoj 2587(最小割是否唯一)

    一开始想了很久,都想不到一个好的方法判断最小割是否唯一,后面无奈只好去看了下题解。 

    正确的方法是:  在用网络流求过最小割后,判断所有的点是不是s能到达,或者能到达t. 如果都满足那么最小割唯一,否则不唯一. 

    我的理解, 可以把这个s不能到达且同时不能到达t的点看成一个集合, 可以知道这个集合流入的边可以等效为饱和弧, 二流出这个点的边也可以等效为饱和弧, 那么就可以用这个点流入的饱和弧代替流出的饱和弧,也就是有多个最小割.


    Unique Attack

    Time Limit: 5 Seconds      Memory Limit: 32768 KB

    N supercomputers in the United States of Antarctica are connected into a network. A network has a simple topology: M different pairs of supercomputers are connected to each other by an optical fibre. All connections are two-way, that is, they can be used in both directions. Data can be transmitted from one computer to another either directly by a fibre, or using some intermediate computers.

    A group of terrorists is planning to attack the network. Their goal is to separate two main computers of the network, so that there is no way to transmit data from one of them to another. For each fibre the terrorists have calculated the sum of money they need to destroy the fibre. Of course, they want to minimize the cost of the operation, so it is required that the total sum spent for destroying the fibres was minimal possible.

    Now the leaders of the group wonder whether there is only one way to do the selected operation. That is, they want to know if there are no two different sets of fibre connections that can be destroyed, such that the main supercomputers cannot connect to each other after it and the cost of the operation is minimal possible.

    Input

    The input file consists of several cases. In each case, the first line of the input file contains N, M, A and B (2 <= N <= 800, 1 <= M <= 10000, 1 <= A,B <= N, A != B), specifying the number of supercomputers in the network, the number of fibre connections, and the numbers of the main supercomputers respectively. A case with 4 zeros indicates the end of file.

    Next M lines describe fibre connections. For each connection the numbers of the computers it connects are given and the cost of destroying this connection. It is guaranteed that all costs are non-negative integer numbers not exceeding 105, no two computers are directly connected by more than one fibre, no fibre connects a computer to itself and initially there is the way to transmit data from one main supercomputer to another.

    Output

    If there is only one way to perform the operation, output "UNIQUE" in a single line. In the other case output "AMBIGUOUS".

    Sample Input

    4 4 1 2
    1 2 1
    2 4 2
    1 3 2
    3 4 1
    4 4 1 2
    1 2 1
    2 4 1
    1 3 2
    3 4 1
    0 0 0 0
    

    Sample Output

    UNIQUE
    AMBIGUOUS
    


    ps: 用了几种方法实现判断点能不能到达t,最后得出重新逆向建一次图然后从t搜一次最快.

    #include <stdio.h>
    #include <string>
    #include <string.h>
    #include <iostream>
    using namespace std;
    #define N 808
    #define M 1000100
    #define INF 0x3fffffff
    struct node
    {
        int to,next,w;
    }edge[M];
    
    int cnt,pre[N];
    int n,m,s,t;
    int nn;
    int lv[N],gap[N];
    int mark[N];
    int mark1[N],sign[N];
    int map[N][N];
    
    void add_edge(int u,int v,int w)
    {
        edge[cnt].to=v;
        edge[cnt].w=w;
        edge[cnt].next=pre[u];
        pre[u]=cnt++;
    }
    
    int sdfs(int k,int w)
    {
        if(k==t) return w;
        int f=0;
        int mi=nn-1;
        for(int p=pre[k];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            if(edge[p].w!=0)
            {
                if(lv[k]==lv[v]+1)
                {
                    int tmp=sdfs(v,min(w-f,edge[p].w));
                    f+=tmp;
                    edge[p].w-=tmp;
                    edge[p^1].w+=tmp;
                    if(f==w||lv[s]==nn) return f;
                }
                if(mi>lv[v]) mi=lv[v];
            }
        }
        if(f==0)
        {
            gap[lv[k]]--;
            if(gap[lv[k]]==0)
            {
                lv[s]=nn;
                return f;
            }
            lv[k]=mi+1;
            gap[lv[k]]++;
        }
        return f;
    }
    
    
    void sap()
    {
        int sum=0;
        nn=n;
        memset(lv,0,sizeof(lv));
        memset(gap,0,sizeof(gap));
        gap[0]=nn;
        while(lv[s]<nn)
        {
            sum+=sdfs(s,INF);
        }
    }
    
    void dfs(int k)
    {
        mark[k]=1;
        for(int p=pre[k];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            if(mark[v]==0&&edge[p].w!=0)
            {
                dfs(v);
            }
        }
    }
    
    /*int dfs1(int k)
    {
        if(k==t) return 1;
        for(int p=pre[k];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            if(edge[p].w==0||sign[v]==1) continue;
            sign[v]=1;
            if(mark1[v]!=-1)
            {
                if(mark1[v]==0) continue;
                else
                {
                    return 1;
                }
            }
            else
            {
                if( dfs1(v)==1 )
                {
                    return 1;
                }
            }
        }
        return 0;
    }*/
    
    /*int dfs1(int k)
    {
        if(k==t) return 1;
        for(int i=pre[k];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].w!=0&&sign[v]==0)
            {
                sign[v]=1;
                if(dfs1(v)==1) return 1;
            }
        }
        return 0;
    }*/
    
    void dfs1(int k)
    {
        mark1[k]=1;
        for(int i=1;i<=n;i++)
        {
            if(map[k][i]!=0&&mark1[i]==0)
            {
                dfs1(i);
            }
        }
    }
    
    int main()
    {
        while(scanf("%d%d%d%d",&n,&m,&s,&t)&&(n+m+s+t))
        {
            cnt=0;
            memset(pre,-1,sizeof(pre));
            memset(mark,0,sizeof(mark));
            for(int i=0;i<m;i++)
            {
                int x,y,w;
                scanf("%d%d%d",&x,&y,&w);
                if(w==0) continue;
                add_edge(x,y,w);
                add_edge(y,x,0);
                
                add_edge(y,x,w);
                add_edge(x,y,0);
            }
            sap();
            dfs(s);
            memset(map,0,sizeof(map));
            for(int i=1;i<=n;i++)
            {
                for(int p=pre[i];p!=-1;p=edge[p].next)
                {
                    int v=edge[p].to;
                    map[v][i]+=edge[p].w; // 逆着建图
                }
            }
            memset(mark1,0,sizeof(mark1));
            dfs1(t);
            /*memset(mark1,-1,sizeof(mark1)); // 记录这个点能不能到达t
            memset(sign,0,sizeof(sign)); // 记忆化搜索必须要一个记录
            for(int i=1;i<=n;i++)
            {            
                memset(sign,0,sizeof(sign));
                if( mark1[i]==-1 )
                {
                    sign[i]=1;
                    mark1[i]=dfs1(i);
                }
            }*/
    
            /*memset(mark1,0,sizeof(mark1));
            for(int i=1;i<=n;i++)
            {
                if(i==s||i==t) continue;
                memset(sign,0,sizeof(sign));
                sign[i]=1;
                if(dfs1(i)==1)
                {
                    mark1[i]=1;
                }
            }*/
            int flag=0;
            for(int i=1;i<=n;i++)
            {
                if(i==s||i==t) continue;
                if(mark[i]==0&&mark1[i]==0)
                {
                    flag=1;// 有多种解决办法
                    break;
                }
            }
            if(flag==1) printf("AMBIGUOUS\n");
            else printf("UNIQUE\n");
        }
        return 0;
    }
  • 相关阅读:
    MySQL批量UPDATE多行记录
    qt 标准对话框
    qt creator 源代码中含有中文编译报错
    qt编译mysql插件
    win7自动登录桌面
    编译QtAV工程库
    Qt Creator 中关于调试器的设置
    QtCreator 添加第三方头文件库文件路径
    Qt 安装一个Service
    Qt 添加启动项
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/2912762.html
Copyright © 2011-2022 走看看