HDU 2448 Mining Station on the Sea(最小费用流+spfa,超了n次的题)

来源:互联网 发布:360手柄淘宝哪里买 编辑:程序博客网 时间:2024/05/02 03:05

Mining Station on the Sea

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


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 61 2 41 3 31 4 41 5 52 5 32 4 31 1 51 5 32 5 32 4 63 1 43 2 2
 

Sample Output
13
 

Source
2008 Asia Regional Harbin
题目大意:海上有M个油田,有N个的船只和港口,N<=M,N艘船停在N个油田,油田和油田之间有距离,油田和港口之间也有距离。现在呢要你把船开会港口,每个港口只能停一艘船,问你怎么停可以使船只行驶的距离最短呢。
解题:因为油田到油田之间船数量流动是没有限制的,所以可以用spfa处理一下N个港口到M个油田的最短距离(油田到油田是双向的,港口到油田是单向的,因为每只般进入港口就不能出来),之后就是 最小费用流了。
#include<stdio.h>#include<string.h>#include<queue>using namespace std;const int MAXN = 310;const int MAXM = 1001000;const int inf = 1<<28;struct EDG{    int to,next,cap;    int cost;  //单价}edg[MAXM];int head[MAXN],eid;int pre[MAXN], cost[MAXN] ; //点0~(n-1)void init(){    eid=0;    memset(head,-1,sizeof(head));}void addEdg(int u,int v,int cap,int cst){    edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost = cst;    edg[eid].cap=cap;  head[u]=eid++;    edg[eid].to=u; edg[eid].next=head[v]; edg[eid].cost = -cst;    edg[eid].cap=0;  head[v]=eid++;}bool inq[MAXN];int q[MAXM];int sNode,eNode;bool spfa(const int& n){    int l=0 , r=0 , i;    memset(inq,0,sizeof(inq));    for(int i=1; i<n; ++i)        cost[i]=inf;    cost[sNode]=0; inq[sNode]=1; pre[sNode]=-1;    q[r++]=sNode;    while(l!=r){        int u=q[l++];        if(l==MAXN)l=0;        inq[u]=0;        i=head[u];        while(~i ){            int v=edg[i].to;            if(edg[i].cap>0 && cost[v]>cost[u]+edg[i].cost){ //在满足可增流的情况下,最小花费                cost[v] = cost[u]+edg[i].cost;                pre[v]=i;   //记录路径上的边                if(!inq[v]){                    if(r==MAXN)r=0;                    q[r++]=v;                    inq[v]=1;                }            }            i=edg[i].next;        }    }    if(cost[eNode]==inf)return 0;    return 1;    //判断有没有增广路}//反回的是最大流,最小花费为minCostvoid minCost_maxFlow(int& minCost,int n){    while(spfa(n)){        minCost+=cost[eNode];        int i=pre[eNode];        while(~i){            edg[i].cap-=1; edg[i^1].cap+=1;            i=pre[edg[i^1].to];        }    }}inline void scanf(int& ans){    char ch=getchar();    while(ch<'0'||ch>'9')        ch=getchar();    ans=0;    while(ch>='0'&&ch<='9'){        ans=ans*10+ch-'0';        ch=getchar();    }}int mapt[MAXN][MAXN],dis[MAXN][MAXN];void spfa(int n,int m){    queue<int>qt;    bool inq[MAXN]={0};    for(int i=m+1; i<=n+m; i++){  //港口        for(int j=1; j<=m; j++)            dis[i][j]=inf;        dis[i][i]=0;        qt.push(i);        while(!qt.empty()){            int u=qt.front(); qt.pop();            inq[u]=0;            for(int v=1; v<=m; v++)  //油田                if(dis[i][v]>dis[i][u]+mapt[u][v]) {                    dis[i][v]=dis[i][u]+mapt[u][v];                    if(!inq[v])                        inq[v]=1,qt.push(v);                }        }    }}int main(){    //输入,初始化init()    int n,m,k,p,a,b,c;    while(~scanf("%d%d%d%d",&n,&m,&k,&p)){        init();        for(int i=1; i<=n+m; i++)            for(int j=1; j<=n+m; j++)                mapt[i][j]=inf;        sNode=0; eNode=n+m+1;        int  i=1;        while(i<=n){            scanf(a);    // 般只所在的油田            addEdg(sNode , a , 1 , 0);            i++;        }        i=1;  while(i<=n) addEdg(i+m , eNode , 1 , 0),i++;        while(k--){            scanf(a);            scanf(b);            scanf(c);            if(mapt[a][b]>c)                mapt[a][b]=mapt[b][a]=c;        }        while(p--){            scanf(a);   //港口            scanf(b);            scanf(c);            if(mapt[a+m][b]>c)                mapt[a+m][b]=c;        }        spfa(n,m);        for(int u=1; u<=m; u++) //油田            for(int v=m+1; v<=m+n; v++) //港口            if(dis[v][u]!=inf)             addEdg(u,v,1,dis[v][u]);        int mincost=0;        minCost_maxFlow( mincost, eNode+1);        printf("%d\n",mincost);    }}


0 0
原创粉丝点击