Mining Station on the Sea
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1910 Accepted Submission(s): 565
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!
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
Source
Recommend
gaojie
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int VM=1010; const int EM=201000; const int INF=0x3f3f3f3f; struct Edge{ int u,v,nxt; int flow,cost; }edge[EM<<1]; int n,cnt,head[VM],val[VM]; int pre[VM],dis[VM],vis[VM]; void addedge(int cu,int cv,int cw,int cc){ edge[cnt].u=cu; edge[cnt].v=cv; edge[cnt].flow=cw; edge[cnt].cost=cc; edge[cnt].nxt=head[cu]; head[cu]=cnt++; edge[cnt].u=cv; edge[cnt].v=cu; edge[cnt].flow=0; edge[cnt].cost=-cc; edge[cnt].nxt=head[cv]; head[cv]=cnt++; } int src,des; int SPFA(){ queue<int> q; while(!q.empty()) q.pop(); for(int i=0;i<=des;i++){ dis[i]=INF; vis[i]=0; pre[i]=-1; } dis[src]=0; vis[src]=1; q.push(src); while(!q.empty()){ int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].v; if(edge[i].flow>0 && dis[v]>dis[u]+edge[i].cost){ dis[v]=dis[u]+edge[i].cost; pre[v]=i; if(!vis[v]){ vis[v]=1; q.push(v); } } } } return pre[des]!=-1; } int mincost(){ int ans=0; while(SPFA()){ int u=des,minf=INF; while(u!=src){ minf=min(minf,edge[pre[u]].flow); u=edge[pre[u]].u; } u=des; while(u!=src){ edge[pre[u]].flow-=minf; edge[pre[u]^1].flow+=minf; u=edge[pre[u]].u; } ans+=dis[des]*minf; } return ans; } int main(){ //freopen("input.txt","r",stdin); int m,k,p; while(~scanf("%d%d%d%d",&n,&m,&k,&p)){ cnt=0; memset(head,-1,sizeof(head)); src=0; des=n+m+1; int u,v,w; for(int i=0;i<n;i++){ scanf("%d",&v); addedge(src,v,1,0); //多源多汇点,把源点S 到 每个船所在的station建一条容量为1,cost为0的单向边 } while(k--){ scanf("%d%d%d",&u,&v,&w); addedge(u,v,m,w); //station之间的边的容量要建为inf。。 addedge(v,u,m,w); } while(p--){ scanf("%d%d%d",&u,&v,&w); addedge(v,u+m,1,w); //port 和 station之间是单向边 } for(int i=1;i<=n;i++) addedge(i+m,des,1,0); //把每个port到 汇点des 建一条容量为1,cost为0的但单向边。 printf("%d\n",mincost()); } return 0; }
相关推荐
求多源点到单终点的最短路(反向建图),ACM竞赛中应用的小程序。
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
2017hdu多校联合训练第一场标程及数据
有题,有解题思路,有解题代码 hdu2516、poj1067和hdu1527、hdu2177、hdu2176等等
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
HDU的ACM,非常的好 涉及了很多算法,例如二分匹配、博弈、组合、最小生成树、搜索、动态规划、贪心算法
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
For a positive integer n, let’s denote function f(n,m) as the m-th smallest integer x that x>n and gcd(x,n)=1. For example, f(5,1)=6 and f(5,5)=11. You are given the value of m and (f(n,m)?n)⊕n,...
hdu 1166线段树代码
自己做的HDU ACM已经AC的题目
HDU最全ac代码