Mining Station on the Sea
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1213 Accepted Submission(s): 316
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
Source
2008 Asia Regional Harbin
Recommend
gaojie
#include<cstdio>
#include<cstring>
const int inf=1<<28-1;
int lx[505],ly[505],map[505][505];
bool sx[505],sy[505];
int link[505],stack[505];
int vis[505];
int n,m,k,p;
int min(int a,int b)
{
if(a>b) return b;
return a;
}
void floyed()
{
for(int k=n+1; k<=n+m; k++) //港口只能去一次,不能作为FLOYED缩点的中间点
for(int i=1; i<=n+m; i++)
for(int j=1; j<=n+m; j++)
if(map[i][k]+map[k][j]<map[i][j]) map[i][j]=map[i][k]+map[k][j];
}
bool path(int k)
{
sx[k]=true;
for(int i=1; i<=n; i++)
{
if(!sy[i]&&(lx[k]+ly[i]==map[k+n][i]))
{
sy[i]=1;
if(link[i]==-1||path(link[i]))
{
link[i]=k;
return true;
}
}
}
return false;
}
int BestMatch()
{
int d,sum;
memset(ly,0,sizeof(ly));
memset(link,-1,sizeof(link));
for(int i=1; i<=n; i++)
{
int k=vis[i];
lx[k]=-inf;
for(int j=1; j<=n; j++)
{
map[k+n][j]=-map[k+n][j];
if(lx[k]<map[k+n][j]) lx[k]=map[k+n][j];
}
}
for(int k=1; k<=n; k++)
{
while(1)
{
int t=vis[k];
memset(sx,0,sizeof(sx));
memset(sy,0,sizeof(sy));
if(path(t)) break;//匹配成功
d=inf;
for(int i=1; i<=n; i++)
{
int t=vis[i];
if(sx[t])
for(int j=1; j<=n; j++)
if(!sy[j]) d=min(d,lx[t]+ly[j]-map[t+n][j]);
}
for(int i=1; i<=n; i++)
{
int t=vis[i];
if(sx[t]) lx[t]-=d;
if(sy[i]) ly[i]+=d;
}
}
}
sum=0;
for(int i=1;i<=n;i++) sum+=map[link[i]+n][i];
return -sum;
}
int main()
{
while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)
{
for(int i=1;i<=n+m;i++)
for(int j=1;j<=n+m;j++)
if(i==j) map[i][j]=0;
else map[i][j]=inf;
for(int i=1;i<=n;i++) scanf("%d",&vis[i]);
for(int i=1;i<=k;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
map[a+n][b+n]=map[b+n][a+n]=min(map[a+n][b+n],c);
}
for(int i=1; i<=p; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);//port a,station b
map[b+n][a]=map[a][b+n]=min(map[b+n][a],c);
}
floyed();
printf("%d/n",BestMatch());
}
return 0;
}