UVA 1349 Optimal Bus Route Design(最小权完美匹配)

来源:互联网 发布:mac 流程图软件 免费 编辑:程序博客网 时间:2024/05/16 05:03
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4095

思路:要求每个点属于且仅属于一个圈,则由于在一个圈中的每个点必定存在后继,又由于每个点只能属于一个圈,所以每个点有且仅有一个后继时满足题意。联想到匹配问题,则对于每条边<i,j>转化为连边<i,j+n>,权值为该边权值,构成一新图。此图为一二分图,则最小费用即为最小权完美匹配值。可用最小费用流解决,所以,设一源点s,汇点t,从s到每个i连边,容量为1,费用为0,;每个j向t连边,容量为1,费用为0;对于i、j,连边,容量为1,费用为该边权值。则此时最小权完美匹配值即为最小费用流,若从s出发的边与汇入t的边均满流,则存在完美匹配,否则不存在,无解。

#include<cstdio>#include<queue>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#define debuusing namespace std;const int maxn=200+50;const int INF=0x3f3f3f3f;struct Edge{    int from,to,cap,flow,cost;};struct MCMF{    int n,m,s,t;    vector<Edge> edges;    vector<int> G[maxn];    int inq[maxn],d[maxn];    int p[maxn],a[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,int cost)    {        edges.push_back((Edge){from,to,cap,0,cost});        edges.push_back((Edge){to,from,0,0,-cost});        m=edges.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    bool BellmanFord(int s,int t,int &flow,int &cost)    {        for(int i=0; i<n; i++) d[i]=INF;        memset(inq,0,sizeof(inq));        d[s]=0,inq[s]=1,p[s]=0,a[s]=INF;        queue<int> Q;        Q.push(s);        while(!Q.empty())        {            int u=Q.front();            Q.pop(),inq[u]=0;            for(int i=0; i<G[u].size(); i++)            {                Edge &e=edges[G[u][i]];                if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)                {                    d[e.to]=d[u]+e.cost;                    p[e.to]=G[u][i];                    a[e.to]=min(a[u],e.cap-e.flow);                    if(!inq[e.to])                    {                        Q.push(e.to);                        inq[e.to]=1;                    }                }            }        }        if(d[t]==INF) return false;        flow+=a[t];        cost+=d[t]*a[t];        int u=t;        while(u!=s)        {            edges[p[u]].flow+=a[t];            edges[p[u]^1].flow-=a[t];            u=edges[p[u]].from;        }        return true;    }    int Mincost(int s,int t)    {        int flow=0,cost=0;        while(BellmanFord(s,t,flow,cost));        return cost;    }};int n;MCMF G;int main(){#ifdef debug    freopen("in.in","r",stdin);#endif // debug    while(scanf("%d",&n)!=EOF&&n)    {        int tmp[150];        G.init(2*n+2);        for(int i=1; i<=n; i++)        {            int x,w;            G.AddEdge(0,i,1,0);            G.AddEdge(i+n,2*n+1,1,0);            tmp[i]=G.m-2;            while(scanf("%d",&x)!=EOF&&x)            {                scanf("%d",&w);                G.AddEdge(i,x+n,1,w);            }        }        int flag=1,ans=G.Mincost(0,2*n+1);        for(int i=0; i<G.G[0].size(); i++)        {            Edge &e=G.edges[G.G[0][i]];            if(e.flow!=1)            {                flag=0;                break;            }        }        for(int i=1; i<=n; i++)        {            Edge &e=G.edges[tmp[i]];            if(e.flow!=1)            {                flag=0;                break;            }        }        if(!flag) printf("N\n");        else printf("%d\n",ans);    }    return 0;}



0 0
原创粉丝点击