【转载】【最短路Floyd+KM 最佳匹配】hdu 2448 Mining Station on the Sea

Mining Station on the Sea

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

13

Source

2008 Asia Regional Harbin

参考博客:

http://www.voidcn.com/article/p-msnnxvzx-da.html

题目大意:有n条船,n个港口,m个点,k条无向边,p条有向边,

其中第一行输入为n条船停靠在的点,现在这些船需要回到港口,而且每个港口只能挺一条船。

k条无向边,表示两个点之间的距离,

p条有向边,表示港口到某个点的距离。

每个船都需要回到港口,问最小距离花费。

思路:

1、因为总点数并不是很大,所以我们可以使用Floyd跑出亮点间最短路。

2、然后设定a[i][j]表示第i个船到第j个港口需要花费的最小消耗,那么我们通过Floyd跑出来的结果对应建图。

3、因为我们要求的是最小匹配,那么我们使用一个极大值(03f3f3f3f-a[i][j])得到一个新的a[i][j],使得原来的较小值变成了较大值,那么此时跑得的最大匹配即最小匹配,那么ans=n*极大值-最大值匹配。

Ac代码:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<iostream>
  4 using namespace std;
  5 #define ll __int64
  6 int map[450][450];
  7 int match[450*450];
  8 int a[450][450];
  9 int lx[450*450];
 10 int ly[450*450];
 11 int vx[450*450];
 12 int vy[450*450];
 13 int at[450];
 14 int low;
 15 int n,m,kk,pp;
 16 int find(int u)
 17 {
 18     vx[u]=1;
 19     for(int i=1;i<=n;i++)
 20     {
 21         if(vy[i]==1)continue;
 22         int tmp=lx[u]+ly[i]-a[u][i];
 23         if(tmp==0)
 24         {
 25             vy[i]=1;
 26             if(match[i]==-1||find(match[i]))
 27             {
 28                 match[i]=u;
 29                 return 1;
 30             }
 31         }
 32         else if(tmp<low)low=tmp;
 33     }
 34     return 0;
 35 }
 36 void KM()
 37 {
 38     memset(match,-1,sizeof(match));
 39     memset(lx,0,sizeof(lx));
 40     memset(ly,0,sizeof(ly));
 41     for(int i=1;i<=n;i++)
 42     {
 43         for(int j=1;j<=n;j++)
 44         {
 45             int u=at[i];
 46             a[i][j]=map[u][j+m];
 47             a[i][j]=0x3f3f3f3f-a[i][j];
 48             lx[i]=max(lx[i],a[i][j]);
 49         }
 50     }
 51     for(int i=1;i<=n;i++)
 52     {
 53         while(1)
 54         {
 55             low=0x3f3f3f3f;
 56             memset(vx,0,sizeof(vx));
 57             memset(vy,0,sizeof(vy));
 58             if(find(i))break;
 59             for(int j=1;j<=n;j++)
 60             {
 61                 if(vx[j])lx[j]-=low;
 62             }
 63             for(int j=1;j<=n;j++)
 64             {
 65                 if(vy[j])ly[j]+=low;
 66             }
 67         }
 68     }
 69     ll sum=0;
 70     for(int i=1;i<=n;i++)
 71     {
 72         sum+=a[match[i]][i];
 73     }
 74     printf("%I64d
",(ll)n*(ll)0x3f3f3f3f-sum);
 75 }
 76 int main()
 77 {
 78     while(~scanf("%d%d%d%d",&n,&m,&kk,&pp))
 79     {
 80         memset(map,0x3f3f3f3f,sizeof(map));
 81         for(int i=1;i<=n;i++)
 82         {
 83             scanf("%d",&at[i]);
 84         }
 85         for(int i=1;i<=kk;i++)
 86         {
 87             int x,y,w;
 88             scanf("%d%d%d",&x,&y,&w);
 89             if(w<map[x][y])
 90             {
 91                 map[x][y]=w;
 92                 map[y][x]=w;
 93             }
 94         }
 95         for(int i=1;i<=pp;i++)
 96         {
 97             int x,y,w;
 98             scanf("%d%d%d",&x,&y,&w);
 99             if(map[y][x+m]>w)
100             {
101                 map[y][x+m]=w;
102             }
103         }
104         for(int i=1;i<=n+m;i++)
105         {
106             for(int j=1;j<=n+m;j++)
107             {
108                 for(int k=1;k<=n+m;k++)
109                 {
110                     map[j][k]=min(map[j][i]+map[i][k],map[j][k]);
111                 }
112             }
113         }
114         KM();
115     }
116 }
KM+flody
原文地址:https://www.cnblogs.com/Osea/p/11397557.html