HDU 2448 mining station on the Sea (billing flow)

Source: Internet
Author: User

Address: HDU 2448

Find n short-circuiting times, find out the most short-circuited n ships to each port, and then use the short-circuited path as the cost, and run the Charge flow once.

The Code is as follows:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;int head[400], cnt, source, sink, a[400], flow, cost;int d[400], f[400], vis[400], cur[400];struct node{    int u, v, cap, cost, next;}edge[1000000];void add(int u, int v, int cap, int cost){    edge[cnt].v=v;    edge[cnt].cap=cap;    edge[cnt].cost=cost;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].v=u;    edge[cnt].cap=0;    edge[cnt].cost=-cost;    edge[cnt].next=head[v];    head[v]=cnt++;}int spfa(){    memset(vis,0,sizeof(vis));    memset(d,INF,sizeof(d));    f[source]=INF;    cur[source]=-1;    d[source]=0;    queue<int>q;    q.push(source);    while(!q.empty())    {        int u=q.front();        q.pop();        vis[u]=0;        for(int i=head[u];i!=-1;i=edge[i].next)        {            int v=edge[i].v;            if(d[v]>d[u]+edge[i].cost&&edge[i].cap)            {                d[v]=d[u]+edge[i].cost;                f[v]=min(f[u],edge[i].cap);                cur[v]=i;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }    if(d[sink]==INF) return 0;    flow+=f[sink];    cost+=f[sink]*d[sink];    for(int i=cur[sink];i!=-1;i=cur[edge[i^1].v])    {        edge[i].cap-=f[sink];        edge[i^1].cap+=f[sink];    }    return 1;}void mcmf(){    flow=cost=0;    while(spfa()) ;    printf("%d\n",cost);}int head1[400], cnt1, dp[400][400];struct node1{    int u, v, w, next;}bian[400];void add1(int u, int v, int w){    bian[cnt1].v=v;    bian[cnt1].w=w;    bian[cnt1].next=head1[u];    head1[u]=cnt1++;}void spfa(int x){    memset(vis,0,sizeof(vis));    memset(dp[x],INF,sizeof(dp[x]));    dp[x][x]=0;    queue<int>q;    q.push(x);    while(!q.empty())    {        int u=q.front();        q.pop();        vis[u]=0;        for(int i=head1[u];i!=-1;i=bian[i].next)        {            int v=bian[i].v;            if(dp[x][v]>dp[x][u]+bian[i].w)            {                dp[x][v]=dp[x][u]+bian[i].w;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }}int main(){    int n, m, k, p, i, j, u, v, w;    while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)    {        memset(head,-1,sizeof(head));        memset(head1,-1,sizeof(head1));        cnt1=0;        cnt=0;        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        while(k--)        {            scanf("%d%d%d",&u,&v,&w);            add1(u,v,w);            add1(v,u,w);        }        while(p--)        {            scanf("%d%d%d",&u,&v,&w);            add1(v,u+m,w);        }        for(i=1;i<=n;i++)        {            spfa(a[i]);        }        source=0;        sink=2*n+1;        for(i=1;i<=n;i++)        {            add(source,i,1,0);            add(i+n,sink,1,0);            for(j=1+m;j<=m+n;j++)            {                if(dp[a[i]][j]!=INF)                {                    add(i,j-m+n,1,dp[a[i]][j]);                    //printf("%d %d %d\n",i,j-m+n,dp[a[i]][j]);                }            }        }        mcmf();    }    return 0;}


HDU 2448 mining station on the Sea (billing flow)

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.