Mining Station on the Sea
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3127 Accepted Submission(s): 950
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
题目大意:
给你n个港口和m个采矿站位于海上,有n搜船开始位于采矿站的位置,给你k条边连接两个采矿站和花费的时间,p条边连接采矿站到港口和花费的时间,问你每搜船到达港口且每个港口只能听一艘船的最少花费
题目思路:
我们首先考虑将港口和采矿站建立二分图,然后就是个二分图带权问题,但是采矿站之间也能走,所以我们可以用spfa预处理每个采矿站到达每个港口的最小花费,然后就可以用KM求解最小花费,这里我们可以看成是n*n的图,只要考虑有船停靠的采矿站!
AC代码:
#include<cstring>
#include<cstdio>
#include<queue>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
using std::queue;
const int maxn = 1000;
const int inf= 1e8;
int n,m,k,p,e;
int hed[maxn],vis[maxn],dis[maxn],in[maxn];
queue<int>q;
struct nod{
int v,w,nex;
}edge[maxn*maxn];
void spfa(int u){
for(int i=1;i<=n+m;i++)dis[i]=inf;
dis[u]=0,vis[u]=1;
q.push(u);
while(!q.empty()){
u = q.front();q.pop();
vis[u]=0;
for(int i=hed[u];~i;i=edge[i].nex){
int v = edge[i].v;
if(dis[v]>dis[u]+edge[i].w){
dis[v]=edge[i].w+dis[u];
if(!vis[v]){
vis[v]=1;
q.push(v);
}
}
}
}
}
void init(){
memset(hed,-1,sizeof(hed));
memset(vis,0,sizeof(vis));
e=1;
}
void add(int u,int v,int w){
edge[e].v=v,edge[e].w=w,edge[e].nex=hed[u],hed[u]=e++;
}
int ex_x[maxn],ex_y[maxn],vis_x[maxn],vis_y[maxn],mp[maxn][maxn],link[maxn],slack[maxn];
void init1(){
for(int i=1;i<=n+m;i++){
ex_y[i]=0,ex_x[i]=-inf;
link[i]=-1;
for(int j=1;j<=n+m;j++){
mp[i][j]=-inf;
}
}
}
bool dfs(int u){
vis_x[u]=1;
for(int i=m+1;i<=n+m;i++){
if(!vis_y[i]&&mp[u][i]!=-inf){
if(ex_x[u]+ex_y[i]==mp[u][i]){
vis_y[i]=1;
if(link[i]==-1||dfs(link[i])){
link[i]=u;
return true;
}
}else {
slack[i]=min(slack[i],ex_x[u]+ex_y[i]-mp[u][i]);
}
}
}
return false;
}
void sove(){
for(int i=0;i<n;i++){
for(int j=m+1;j<=n+m;j++)slack[j]=inf;
while(1){
memset(vis_x,0,sizeof(vis_x));
memset(vis_y,0,sizeof(vis_y));
if(dfs(in[i]))break;
int d = inf;
for(int j=m+1;j<=n+m;j++)if(!vis_y[j])d=min(d,slack[j]);
for(int j=1;j<=n+m;j++){
if(vis_x[j])ex_x[j]-=d;
if(vis_y[j])ex_y[j]+=d;
else slack[j]-=d;
}
}
}
int res=0;
for(int i=m+1;i<=n+m;i++){
if(link[i]!=-1)res+=mp[link[i]][i];
}
printf("%d\n",-res);
}
int main()
{
while(~scanf("%d%d%d%d",&n,&m,&k,&p))
{
init();
init1();
for(int i=0;i<n;i++)scanf("%d",&in[i]);
while(k--){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
while(p--){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
add(v,u+m,w);
}
for(int i=0;i<n;i++){
int u = in[i];
spfa(u); //spfa预处理
for(int j=m+1;j<=n+m;j++){
mp[u][j]=max(mp[u][j],-dis[j]); //建立二分图
ex_x[u]=max(ex_x[u],mp[u][j]);
}
}
sove();
}
return 0;
}