zoj 2587 Unique Attack(判断最小割的唯一性)

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


两台计算机之间通过光纤连接  摧毁光纤需要一定的费用  

现知道计算机之间需要摧毁光纤的费用 要使得A B主机之间切断联系 

且使得摧毁的光纤的费用最小 问摧毁的方案是否唯一


双向建图

如何判断最小割的唯一性:

从源点和汇点分别dfs 遍历所有非满流的点 求这些点的个数

如果两次dfs之后求得的点的个数与所有点的个数相等 则唯一

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl;
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

struct Edge
{
    int from,to,cap,flow;
    bool operator <(const Edge e) const
    {
        if(e.from!=from)  return from<e.from;
        else return to<e.to;
    }
    Edge() {}
    Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow) {}
};

struct Dinic
{
    vector<Edge> edges;
    vector<int> G[MAXN];
    bool vis[MAXN];//BFS使用
    int d[MAXN];   //从起点到i的距离
    int cur[MAXN]; //当前弧下标
    int n,m,s,t,maxflow;   //节点数 边数(包括反向弧) 源点编号和弧点编号
    int cnt1,cnt2;//从源点
    bool vis1[MAXN];

    void init(int n)
    {
        this->n=n;
        for(int i=0;i<=n;i++)
            G[i].clear();
        edges.clear();
    }

    void addedge(int from,int to,int cap)
    {
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,cap,0));//当是无向图时,反向边容量也是cap,有向边时,反向边容量是0
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    void dfs1(int u)
    {
        cnt1++;
        vis1[u]=1;
        for(int i=0;i<G[u].size();i++)
        {
            Edge e=edges[G[u][i]];
            if(!vis1[e.to]&&e.cap>e.flow)
                dfs1(e.to);
        }
    }

    void dfs2(int u)
    {
        cnt2++;
        vis1[u]=1;
        for(int i=0;i<G[u].size();i++)
        {
            Edge e=edges[G[u][i]^1];
            if(!vis1[e.from]&&e.cap>e.flow)
                dfs2(e.from);
        }
    }

    bool bfs()
    {
        MEM(vis,0);
        MEM(d,-1);
        queue<int> q;
        q.push(s);
        d[s]=maxflow=0;
        vis[s]=1;
        while(!q.empty())
        {
            int u=q.front(); q.pop();
            int sz=G[u].size();
            for(int i=0;i<sz;i++)
            {
                Edge e=edges[G[u][i]];
                if(!vis[e.to]&&e.cap>e.flow)
                {
                    d[e.to]=d[u]+1;
                    vis[e.to]=1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int u,int a)
    {
        if(u==t||a==0)  return a;
        int sz=G[u].size();
        int flow=0,f;
        for(int &i=cur[u];i<sz;i++)
        {
            Edge &e=edges[G[u][i]];
            if(d[u]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[u][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0)  break;
            }
        }
        return flow;
    }

    int Maxflow(int s,int t)
    {
        this->s=s; this->t=t;
        int flow=0;
        while(bfs())
        {
            MEM(cur,0);
            flow+=dfs(s,INF);
        }
        return flow;
    }

}Dic;

int main()
{
//    fread;
    int n,m,a,b;
    while(scanf("%d%d%d%d",&n,&m,&a,&b)!=EOF)
    {
        if(n==0&&m==0&&a==0&&b==0)
            break;
        int s=a,t=b;
        Dic.init(n);
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            Dic.addedge(u,v,w);

//            Dic.addedge(v,u,w);
        }
        Dic.cnt1=Dic.cnt2=0;
        MEM(Dic.vis1,0);
//        MEM(Dic.vis2,0);
//        cout<<Dic.Maxflow(s,t)<<endl;
        int x=Dic.Maxflow(s,t);
        Dic.dfs1(s);
        Dic.dfs2(t);
//        cout<<Dic.cnt1+Dic.cnt2<<endl;
        if(Dic.cnt1+Dic.cnt2==n)
            puts("UNIQUE");
        else puts("AMBIGUOUS");
    }
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值