Hdu2448 billing Flow

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2448

It's similar to the one who came home on the map ~~~

There is no direction between stations, and the port and station are directed, because they cannot come in.

Create a cost flow diagram.

#include <cstring>#include <cstdio>#include <queue>#include <iostream>#define inf 0x3f3f3f3f#define MAXN 3000#define MAXM 300000using namespace std;struct node{    int u,v,f,c;};node e[MAXM];int first[MAXN],next[MAXM],cc;int inq[MAXN],pre[MAXN],preedge[MAXN],d[MAXN];inline void add_edge(int u,int v,int f,int c){    e[cc].u=u;    e[cc].v=v;    e[cc].f=f;    e[cc].c=c;    next[cc]=first[u];    first[u]=cc;    cc++;    e[cc].v=u;    e[cc].u=v;    e[cc].f=0;    e[cc].c=-c;    next[cc]=first[v];    first[v]=cc;    cc++;}int SPFA(int s,int t){    memset(inq,0,sizeof(inq));    memset(d,inf,sizeof(d));    memset(pre,-1,sizeof(pre));    memset(preedge,-1,sizeof(preedge));    d[s]=0;    queue<int> q;    q.push(s);    while(!q.empty())    {        int u=q.front();        q.pop();        inq[u]=0;        int i;        for(i=first[u];i!=-1;i=next[i])        {            int v=e[i].v;            if(e[i].f)            {                if(d[v]>d[u]+e[i].c)                {                    d[v]=d[u]+e[i].c;                    pre[v]=u;                    preedge[v]=i;                    if(!inq[v])                    {                        inq[v]=1;                        q.push(v);                    }                }            }        }    }    if(d[t]>=inf)        return 0;    else        return 1;}int Min_Cost_Flow(int s,int t){    int ans_flow=0,ans_cost=0,mm,tmp;    while(SPFA(s,t))    {        mm=inf;        int u=t;        while(pre[u]!=-1)        {            tmp=preedge[u];            mm=min(mm,e[tmp].f);            u=pre[u];        }        u=t;        while(pre[u]!=-1)        {            tmp=preedge[u];            e[tmp].f-=mm;            e[tmp^1].f+=mm;            u=pre[u];        }        ans_flow+=mm;        ans_cost+=mm*d[t];    }    return ans_cost;}int main(){    int n,m,k,p;    while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)    {        memset(first,-1,sizeof(first));        memset(next,-1,sizeof(next));        cc=0;        int i;        int s=0,t=n+m+1;        for(i=1;i<=n;i++)        {            add_edge(i+m,t,1,0);            int a;            scanf("%d",&a);            add_edge(0,a,1,0);        }        for(i=1;i<=k;i++)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            add_edge(a,b,inf,c);            add_edge(b,a,inf,c);        }        for(i=1;i<=p;i++)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            add_edge(b,a+m,1,c);        }        int res=Min_Cost_Flow(s,t);        printf("%d\n",res);    }    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.