首页 > 代码库 > HDU 2448 Mining Station on the Sea(费用流)

HDU 2448 Mining Station on the Sea(费用流)

题目地址:HDU 2448

求n次最短路,将n艘船到各港口的最短路求出来,然后用最短路当费用,跑一次费用流。

代码如下:

#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(费用流)