Mining Station on the SeaTime
limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2572 Accepted Submission (s): 775
Problem DescriptionThe Ocean is a treasure house of resources and the development of human society comes to depend D more on it. In order to develop and utilize marine resources, it's necessary to build mining stations on the sea. However, due to seabed mineral resources, the radio signal in the sea are often so weak, this not all the mining stations CA n Carry out direct communication. However communication is indispensable, every, mining stations must being able to communicate with all other (either dire Ctly or through other one or more mining stations). To meet the need of transporting the exploited resources up to the land for get put into use, there build n ports correspon Dently 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 F Or ships, ships is only allowed to sail between mining stations which can communicate with each other directly.
The mining is arduous and people does this job need proper rest (that's, 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 is scattered in different stations and now they has to go back to the port, in addition, a port can only Accommodat e one vessel. Now all the vessels would start to return, how to choose their navigation routes to make the total sum of their sailing rou TES minimal.
Notice that once the ship entered the port, it'll not come out!
Inputthere is several test cases. Every test case begins with four integers in one line, n (1 = <n <=), m (n <= m <=), K and P. N indica TES 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 is n-integers, each one indicating one station, 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 commun Ication 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 D Irect communication between Port D and Mining station E and the distance between them are f. In addition, mining stations is represented by numbers from 1 to M., and Ports 1 to N. Input is terminated by end of file.
Outputeach 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
Source2008 Asia Regional HarbinThe main topic: There are M oil fields on the sea, there are n ships and ports, the N<=m,n ship stops in n oilfields, there is a distance between the oil field and the oil field, and there is a distance between the oil field and the port. Now I want you to take the boat to a meeting port, each port can only stop a boat, ask you how to stop can make the shortest distance of the boat travel.
Problem Solving: Because the oil field to the oil field between the number of vessels flow is unlimited, so you can use SPFA to deal with n ports to M oil field of the shortest distance (oilfield to oilfield is bidirectional, port to oilfield is one-way, because every like into the port can not come out), after that is the minimum cost flow.
#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; Unit price}edg[maxm];int Head[maxn],eid;int PRE[MAXN], COST[MAXN]; Point 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) {//The minimum cost cost[v] = Cost[u]+edg[i].cost in the case of an additive flow; Pre[v]=i; The Edge if (!inq[v]) {if (R==MAXN) r=0 on the record path; Q[r++]=v; Inq[v]=1; }} I=edg[i].next; }} if (Cost[enode]==inf) return 0; return 1; Determine if there is no augmented path}//reverse is the maximum flow, the minimum cost is mincostvoid mincost_maxflow (int& mincost,int N) {while (SPFA (n)) {Mincost+=cost[eno De]; 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++) {//port 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++)//Oilfield 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 () {//input, initialize 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) in the same field; 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); Port 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++)//oilfield for (int v=m+1; v<=m+n; v++)//Port if (Dis[v][u]!=inf) a DDEDG (U,v,1,dis[v][u]); int mincost=0; Mincost_maxflow (Mincost, enode+1); printf ("%d\n", mincost); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2448 Mining Station on the Sea (minimum cost flow +SPFA, Super n Times)