hdu 2448 Mining Station on the Sea【网络费用流】

Mining Station on the Sea

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2371    Accepted Submission(s): 732


Problem Description
The ocean is a treasure house of resources and the development of human society comes to depend more and more on it. In order to develop and utilize marine resources, it is necessary to build mining stations on the sea. However, due to seabed mineral resources, the radio signal in the sea is often so weak that not all the mining stations can carry out direct communication. However communication is indispensable, every two mining stations must be able to communicate with each other (either directly or through other one or more mining stations). To meet the need of transporting the exploited resources up to the land to get put into use, there build n ports correspondently along the coast and every port can communicate with one or more mining stations directly.

Due to the fact that some mining stations can not communicate with each other directly, for the safety of the navigation for ships, ships are only allowed to sail between mining stations which can communicate with each other directly.

The mining is arduous and people do this job need proper rest (that is, to allow the ship to return to the port). But what a coincidence! This time, n vessels for mining take their turns to take a rest at the same time. They are scattered in different stations and now they have to go back to the port, in addition, a port can only accommodate one vessel. Now all the vessels will start to return, how to choose their navigation routes to make the total sum of their sailing routes minimal.

Notice that once the ship entered the port, it will not come out!
 

Input
There are several test cases. Every test case begins with four integers in one line, n (1 = <n <= 100), m (n <= m <= 200), k and p. n indicates n vessels and n ports, m indicates m mining stations, k indicates k edges, each edge corresponding to the link between a mining station and another one, p indicates p edges, each edge indicating the link between a port and a mining station. The following line is n integers, each one indicating one station that one vessel belongs to. Then there follows k lines, each line including 3 integers a, b and c, indicating the fact that there exists direct communication between mining stations a and b and the distance between them is c. Finally, there follows another p lines, each line including 3 integers d, e and f, indicating the fact that there exists direct communication between port d and mining station e and the distance between them is f. In addition, mining stations are represented by numbers from 1 to m, and ports 1 to n. Input is terminated by end of file.

 

Output
Each test case outputs the minimal total sum of their sailing routes.
 

Sample Input
  
  
3 5 5 6 1 2 4 1 3 3 1 4 4 1 5 5 2 5 3 2 4 3 1 1 5 1 5 3 2 5 3 2 4 6 3 1 4 3 2 2
 

Sample Output
  
  
13
 分析:题意:有n艘船和n个港口,m割油井,n艘船在m个油井出,油井之间有距离(双向),油井到港口有距离(单向),求所有船开到港口的最小距离,每个港口对应一艘船,路径走一次。
本题可以直接费用流建图(由于英语知识不好,加边参数加错了,坑了三小时),s连油井,t连港口,还有其它边,费用为距离。
除此之外,还可以费用流+最短行时间优化,建图方式为,s与船连接,t与港口,费用为船到各个港口的最短路。
代码示例
一【未优化】
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define Min(a,b) a<b?a:b
#define inf 0x3f3f3f3f
#define maxn 2000000
#define maxm 3000
using namespace std;
typedef struct
{
    int from,to,next;
    int val,cost;
}node;
node E[maxn];
int head[maxm],dis[maxm],visit[maxm],pre[maxm],pos[maxm],cnt;
int ho[maxm];
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void add(int from,int to,int val,int cost)
{
    E[cnt].from=from,E[cnt].to=to,E[cnt].val=val,E[cnt].cost=cost;
    E[cnt].next=head[from],head[from]=cnt++;
    E[cnt].from=to,E[cnt].to=from,E[cnt].val=0,E[cnt].cost=-cost;
    E[cnt].next=head[to],head[to]=cnt++;
}
bool spfa(int s,int t,int n)
{
    int to,val,cost;
    for(int i=0;i<=n;i++)
    {
        dis[i]=inf,visit[i]=0,pre[i]=-1;
    }
    dis[s]=0,visit[s]=1,pre[s]=s;
    queue<int>Q;
    Q.push(s);
    while(!Q.empty())
    {
        int k=Q.front();
        Q.pop();
        visit[k]=0;
        for(int i=head[k];i!=-1;i=E[i].next)
        {
            to=E[i].to,val=E[i].val,cost=E[i].cost;
            if(val>0&&dis[k]+cost<dis[to])
            {
                dis[to]=dis[k]+cost;
                pre[to]=k;
                pos[to]=i;
                if(visit[to])
                continue;
                visit[to]=1;
                Q.push(to);
            }
        }
    }
    if(pre[t]!=-1&&dis[t]<inf)
    return true;
    return false;
}
int MinCostFlow(int s,int t,int n)
{
    int netflow=0,costflow=0,min;
    while(spfa(s,t,n))
    {  
        min=inf;
        for(int i=t;i!=s;i=pre[i])
        {
            min=Min(min,E[pos[i]].val);
        }
        netflow+=min;
        costflow+=min*dis[t];
        for(int i=t;i!=s;i=pre[i])
        {
            E[pos[i]].val-=min;
            E[pos[i]^1].val+=min;
        }
    }
    return costflow;
}
int main()
{
    int n,m,k,p,s,t;
    int a,b,c;
    while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)
    {
        init();
        for(int i=1;i<=n;i++)
        scanf("%d",&ho[i]);
        for(int i=0;i<k;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,inf,c);
            add(b,a,inf,c);
        }
        for(int i=0;i<p;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(b,a+m,1,c);
        }
        s=n+m+1,t=n+m+2;
        for(int i=1;i<=n;i++)
        {
            add(s,ho[i],1,0);
            add(i+m,t,1,0);
        }
        printf("%d\n",MinCostFlow(s,t,t+10));
    }
    return 0;
}


二【优化】
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define Min(a,b) a<b?a:b
#define inf 0x3f3f3f3f
#define maxn 2000000
#define maxm 1000
using namespace std;
typedef struct
{
    int from,to,next;
    int val,cost;
}node;
node E[maxn];
int head[maxm],dis[maxm],visit[maxm],pre[maxm],pos[maxm],cnt;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void add(int from,int to,int val,int cost)
{
    E[cnt].from=from,E[cnt].to=to,E[cnt].val=val,E[cnt].cost=cost;
    E[cnt].next=head[from],head[from]=cnt++;
    E[cnt].from=to,E[cnt].to=from,E[cnt].val=0,E[cnt].cost=-cost;
    E[cnt].next=head[to],head[to]=cnt++;
}
bool spfa(int s,int t,int n)
{
    int to,val,cost;
    for(int i=0;i<=n;i++)
    {
        dis[i]=inf,visit[i]=0,pre[i]=-1;
    }
    dis[s]=0,visit[s]=1,pre[s]=s;
    queue<int>Q;
    Q.push(s);
    while(!Q.empty())
    {
        int k=Q.front();
        Q.pop();
        visit[k]=0;
        for(int i=head[k];i!=-1;i=E[i].next)
        {   
            to=E[i].to,val=E[i].val,cost=E[i].cost;
            if(val>0&&dis[k]+cost<dis[to])
            {  
                dis[to]=dis[k]+cost;
                pre[to]=k;
                pos[to]=i;
                if(visit[to])
                continue;
                visit[to]=1;
                Q.push(to);
            }
        }
    }
    if(pre[t]!=-1&&dis[t]<inf)
    return true;
    return false;
}
int MinCostFlow(int s,int t,int n)
{
    int costflow=0,netflow=0,min;
    while(spfa(s,t,n))
    {   
        min=inf;
        for(int i=t;i!=s;i=pre[i])
        {
            min=Min(min,E[pos[i]].val);
        }
        netflow+=min;
        costflow+=min*dis[t];
        for(int i=t;i!=s;i=pre[i])
        {
            E[pos[i]].val-=min;
            E[pos[i]^1].val+=min;
        }
    }
    return costflow;
}
/fei yong liu/
typedef struct
{
    int to,next,w;
}nodf;
nodf Es[maxn];
int heads[maxm],visits[maxm],diss[maxm],cnts;
void inits()
{
    memset(heads,-1,sizeof(heads));
    cnts=0;
}
void adds(int from,int to,int w)
{
    Es[cnts].to=to,Es[cnts].w=w,Es[cnts].next=heads[from],heads[from]=cnts++;
}
void spfas(int s,int n)
{   
    int to,w;
    for(int i=0;i<=n;i++)
    {
        diss[i]=inf,visits[i]=0;
    }
    diss[s]=0,visits[s]=1;
    queue<int>Q;
    Q.push(s);
    while(!Q.empty())
    {
        int k=Q.front();
        Q.pop();
        visits[k]=0;
        for(int i=heads[k];i!=-1;i=Es[i].next)
        {   
            to=Es[i].to,w=Es[i].w;
            if(diss[k]+w<diss[to])
            {
                diss[to]=diss[k]+w;
                if(visits[to])     
                continue;
                visits[to]=1;
                Q.push(to);
            }
        }
    }
}
//zui duan lu///
int ho[maxm];
int main()
{
    int n,m,k,p,s,t;
    int a,b,c;
    while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)
    {
        for(int i=1;i<=n;i++)
        scanf("%d",&ho[i]);
        inits();
        for(int i=1;i<=k;i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            adds(a,b,c);
            adds(b,a,c);
        }
        for(int i=1;i<=p;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            adds(b,a+m,c);
        }
        init();
        s=0,t=2*n+1;
        for(int i=1;i<=n;i++)
        {   
            add(s,i,1,0);
            add(i+n,t,1,0);
            spfas(ho[i],n+m+100);
            for(int j=1;j<=n;j++)
            {    
                if(diss[m+j]<inf)
                {   
                    add(i,n+j,1,diss[m+j]);
                }
                
            }
         }
        printf("%d\n",MinCostFlow(s,t,t+10));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值