您的位置:首页 > 其它

HDU 2448 Mining Station on the Sea(Floyd+最优匹配)

2016-02-19 14:17 483 查看
题意:给你一个由N个港口和M个海上油田构成的连通无向图(给出了图中所有的边和权值),现在给你N个船所在的油田编号,问你让这N条船,每条都回到1个港口去(每个港口最多只能容纳一条船),问你这N条船行走的总距离最短是多少?

思路:其实每条船回到任意一个港口去都有一个距离(用Floyd算法算出的最短距离). 建立二分图: 我们把二分图左边放N个港口,右边放N条船,如果第j条船到第i个港口的距离为x,那么就在右j点与左i点之间连一条权值为x的边.答案即为求
该二分图的最优匹配权值是多少?

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100+5;

struct Max_Match
{
int n,W[maxn][maxn];
int Lx[maxn],Ly[maxn];
bool S[maxn],T[maxn];
int left[maxn];

bool match(int i)
{
S[i]=true;
for(int j=1;j<=n;j++)if(Lx[i]+Ly[j]==W[i][j] && !T[j])
{
T[j]=true;
if(left[j]==-1 || match(left[j]))
{
left[j]=i;
return true;
}
}
return false;
}

void update()
{
int a=1<<30;
for(int i=1;i<=n;i++)if(S[i])
for(int j=1;j<=n;j++)if(!T[j])
a=min(a, Lx[i]+Ly[j]-W[i][j]);
for(int i=1;i<=n;i++)
{
if(S[i]) Lx[i] -=a;
if(T[i]) Ly[i] +=a;
}
}

int solve(int n)
{
this->n=n;
memset(left,-1,sizeof(left));
for(int i=1;i<=n;i++)
{
Lx[i]=Ly[i]=0;
for(int j=1;j<=n;j++)
Lx[i]=max(Lx[i],W[i][j]);
}

for(int i=1;i<=n;i++)
{
while(true)
{
for(int j=1;j<=n;j++) S[j]=T[j]=false;
if(match(i)) break;
else update();
}
}
int ans=0;
for(int i=1;i<=n;i++)
ans += W[left[i]][i];
return -ans;//注意取负数
}
}KM;

#define INF 1e9
int dist[300+10][300+10];
void floyd(int n)
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(dist[i][k]< INF && dist[k][j]<INF)
dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
}

int main()
{
int n,m,k,p;
int station_id[maxn];
while(scanf("%d%d%d%d",&n,&m,&k,&p)==4)
{
for(int i=1;i<=n;i++)
scanf("%d",&station_id[i]);
for(int i=1;i<=n+m;i++)
for(int j=1;j<=n+m;j++)
dist[i][j]= i==j?0:INF;
while(k--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
dist[u+n][v+n]=dist[v+n][u+n]=w;
}
while(p--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
dist[u][v+n]=w;//港口到油田是单向的
}
floyd(n+m);

for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
KM.W[i][j] = -dist[i][station_id[j]+n];//注意取负数
printf("%d\n",KM.solve(n));
}
return 0;
}


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

13
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: