hdu 2448 Mining Station on the Sea km算法+最短路

11 篇文章 0 订阅
1 篇文章 0 订阅

Mining Station on the Sea

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3081    Accepted Submission(s): 932


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

题意:n个港口,n条船,m个矿场,船只能在有连边的两个地区之间航行,给了港口之间的边的权值,给了港口到某些矿场之间边的权值,刚开始给了船在哪n个矿场,船进了港口就无法出来。问最少的权值使船全部进港口。

思路:建图过程,港口之间建无向边,矿场到港口之间建有向边(不能出港口),跑每个起点的最短路,建立每个起点和每个能到的港口之间的最短路图(边的权值是负的最短路值),跑一边km即可。

ps:其实每道权值匹配的题都卡费用流我是很不服气的。

/*
 * Author:  ktmzgl
 * Created Time:  2016/10/10 16:29:52
 * File Name: F:\Vim\hdu_2448_km+shortestway.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
using namespace std;
const int N=310;
const int INF=0x3f3f3f3f;  
int nx,ny;//两边的点数
int g[N][N];//二分图描述
int w[N][N];
int linker[N],lx[N],ly[N];//y中各点匹配状态,x,y中的点标号
int slack[N];
bool visx[N],visy[N];
int n,m,k,p;
int st[N];
int v[N];
int d[N];
void dij(int s,int n)
{
	memset(v, 0, sizeof(v));//矩阵存储
for(int i = 0; i < n; i++) d[i] = (i==s ? 0 : INF);
for(int i = 0; i < n; i++) {
  int x, m = INF;
  for(int y = 0; y < n; y++) if(!v[y] && d[y]<=m) m = d[x=y];
  v[x] = 1;
  for(int y = 0; y < n; y++) d[y] = min(d[y], d[x] + w[x][y]);
}
}

/*  KM算法
 *   复杂度O(nx*nx*ny)
 *  求最大权匹配
 *   若求最小权匹配,可将权值取相反数,结果取相反数
 *  点的编号从0开始
 */

bool DFS(int x)
{
    visx[x] = true;
    for(int y = 0; y < ny; y++)
    {
        if(visy[y])continue;
        int tmp = lx[x] + ly[y] - g[x][y];
        if(tmp == 0)
        {
            visy[y] = true;
            if(linker[y] == -1 || DFS(linker[y]))
            {
                linker[y] = x;
                return true;
            }
        }
        else if(slack[y] > tmp)
            slack[y] = tmp;
    }
    return false;
}
int KM()
{
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));
    for(int i = 0;i < nx;i++)
    {
        lx[i] = -INF;
        for(int j = 0;j < ny;j++)
            if(g[i][j] > lx[i])
                lx[i] = g[i][j];
    }
    for(int x = 0;x < nx;x++)
    {
        for(int i = 0;i < ny;i++)
            slack[i] = INF;
        while(true)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(DFS(x))break;
            int d = INF;
            for(int i = 0;i < ny;i++)
                if(!visy[i] && d > slack[i])
                    d = slack[i];
            for(int i = 0;i < nx;i++)
                if(visx[i])
                    lx[i] -= d;
            for(int i = 0;i < ny;i++)
            {
                if(visy[i])ly[i] += d;
                else slack[i] -= d;
            }
        }
    }
    int res = 0;
    for(int i = 0;i < ny;i++)
        if(linker[i] != -1)
            res += g[linker[i]][i];
    return res;
}

int main()
{
	while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%d",&st[i]);
			st[i]--;
		}
		memset(w,INF,sizeof(w));
		memset(g,0,sizeof(g));
		for(int i=0;i<k;i++)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			w[x-1][y-1]=z;
			w[y-1][x-1]=z;
		}
		for(int i=0;i<p;i++)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			w[y-1][x-1+m]=z;
		}
		for(int i=0;i<n;i++)
		{
			dij(st[i],n+m);
			/*cout<<"start"<<st[i]<<endl;
			for(int k=0;k<m+n;k++)
		{
			cout<<k<<" "<<d[k]<<endl;
		}*/
			for(int j=m;j<m+n;j++)
			{
				if(d[j]<INF)
					g[i][j-m]=-d[j];
			}
		}
		nx=n,ny=n;
		
		cout<<-KM()<<endl;
	}
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值