Hdu1853/HDU 3488 directed graph, with K circles covering all vertices once // fee stream

Source: Internet
Author: User

This is the Basic Charge flow question type, Split points, create a bipartite graph, and run the maximum flow with the minimum cost. If the maximum stream is N, it indicates that the maximum match is N. All vertices are involved. The inbound and outbound degrees of each vertex are 1, so they are loops.

Hard work is required for weak dishes!


#include<cstdio>#include<iostream>#include<queue>#include<cstring>using namespace std;const int inf=0x3f3f3f3f;int nume=0;int e[50000][4];int head[500];int n,m;void inline  adde(int i,int j,int c,int w){    e[nume][0]=j;e[nume][1]=head[i];head[i]=nume;    e[nume][2]=c;e[nume++][3]=w;    e[nume][0]=i;e[nume][1]=head[j];head[j]=nume;    e[nume][2]=0;e[nume++][3]=-w;}int inq[500];int pre[500];int prv[500];int d[500];bool spfa(int &sum,int &sumflow){    for(int i=0;i<=2*n+2;i++)          {              inq[i]=0;              d[i]=inf;          }    queue<int>q;    q.push(0);    inq[0]=1;    d[0]=0;    while(!q.empty())    {        int cur=q.front();        q.pop();        inq[cur]=0;        for(int i=head[cur];i!=-1;i=e[i][1])        {            int v=e[i][0];            if(e[i][2]>0&&d[cur]+e[i][3]<d[v])            {                d[v]=d[cur]+e[i][3];                pre[v]=i;                prv[v]=cur;                if(!inq[v])                {                    q.push(v);                    inq[v]=1;                }            }        }    }    if(d[2*n+2]==inf)return 0;    int cur=2*n+2;int minf=inf;    while(cur!=0)    {        minf=e[pre[cur]][2]<minf?e[pre[cur]][2]:minf;        cur=prv[cur];    }     cur=2*n+2;    while(cur!=0)    {        e[pre[cur]][2]-=minf;        e[pre[cur]^1][2]+=minf;        cur=prv[cur];    }    sumflow+=minf;    sum+=minf*d[2*n+2];    return 1;}int mincost(int &sumflow){    int sum=0;    while(spfa(sum,sumflow));    return sum;}void init(){    nume=0;    memset(head,-1,sizeof(head));}int main(){    while(~scanf("%d%d",&n,&m))    {        init();        int a,b,c;        for(int i=0;i<m;i++)        {           scanf("%d%d%d",&a,&b,&c);            adde(a,b+n,1,c);        }        for(int i=1;i<=n;i++)        {            adde(0,i,1,0);            adde(i+n,2*n+1,1,0);        }        adde(2*n+1,2*n+2,n,0);        int sumflow=0;        int ans=mincost(sumflow);        if(sumflow!=n)        printf("-1\n");        else        {            printf("%d\n",ans);        }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.