Hdu2448/fee stream/Harbin division C

Source: Internet
Author: User

Question (Self) Wrong (English) Comprehensive (language) complex (too) complex (poor), relationship processing for half an hour + translation to understand, after understanding, directly create a graph and kill the traffic. /Simple question.

2a: in some cases, you need to build bidirectional edges for interworking!

#include<cstdio>#include<iostream>#include<queue>#include<cstring>#include<string>using namespace std;const int maxv=400;const int maxe=400*400*2+800;const int inf=0x3f3f3f3f;int nume=0;int e[maxe][4];int head[maxv];int n,m,k,p;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[maxv];int pre[maxv];int prv[maxv];int d[maxv];int froms[maxv];bool spfa(int &sum,int &flow){    int s=0,t=n+m+1;    for(int i=0;i<=t;i++)          {              inq[i]=0;              d[i]=inf;          }    queue<int>q;    q.push(s);    inq[s]=1;    d[s]=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[t]==inf)return 0;    int cur=t;    int minf=inf;    while(cur!=s)    {        int fe=pre[cur];        minf=e[fe][2]<minf?e[fe][2]:minf;        cur=prv[cur];    }     cur=t;    while(cur!=s)    {        e[pre[cur]][2]-=minf;        e[pre[cur]^1][2]+=minf;        cur=prv[cur];    }    flow+=minf;    sum+=d[t]*minf;    return 1;}int mincost(int &flow){    int sum=0;   while(spfa(sum,flow));    return sum;}void init(){    nume=0;    for(int i=0;i<=n+m+5;i++)       head[i]=-1;}void read_build(){     for(int j=0;j<n;j++)          scanf("%d",&froms[j]);      int aa,bb,cc;     for(int i=0;i<k;i++)     {        scanf("%d%d%d",&aa,&bb,&cc);        adde(aa,bb,inf,cc);        adde(bb,aa,inf,cc);     }     for(int i=0;i<p;i++)     {        scanf("%d%d%d",&aa,&bb,&cc);        adde(bb,aa+m,inf,cc);      //   adde(aa+m,bb,inf,cc);     }      for(int i=0;i<n;i++)      {          adde(0,froms[i],1,0);          adde(m+1+i,n+m+1,1,0);      }    /* for(int i=0;i<=2*n+1;i++)       for(int j=head[i];j!=-1;j=e[j][1])       {           printf("%d->%d:f %dw %d\n",i,e[j][0],e[j][2],e[j][3]);       }*/}int main(){    while(~scanf("%d%d%d%d",&n,&m,&k,&p))    {        init();        read_build();        int flow=0;        int ans=mincost(flow);         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.