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

原创 2016年08月31日 14:41:26

Mining Station on the Sea

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

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

 

题目大意:有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代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
int map[450][450];
int match[450*450];
int a[450][450];
int lx[450*450];
int ly[450*450];
int vx[450*450];
int vy[450*450];
int at[450];
int low;
int n,m,kk,pp;
int find(int u)
{
    vx[u]=1;
    for(int i=1;i<=n;i++)
    {
        if(vy[i]==1)continue;
        int tmp=lx[u]+ly[i]-a[u][i];
        if(tmp==0)
        {
            vy[i]=1;
            if(match[i]==-1||find(match[i]))
            {
                match[i]=u;
                return 1;
            }
        }
        else if(tmp<low)low=tmp;
    }
    return 0;
}
void KM()
{
    memset(match,-1,sizeof(match));
    memset(lx,0,sizeof(lx));
    memset(ly,0,sizeof(ly));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int u=at[i];
            a[i][j]=map[u][j+m];
            a[i][j]=0x3f3f3f3f-a[i][j];
            lx[i]=max(lx[i],a[i][j]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        while(1)
        {
            low=0x3f3f3f3f;
            memset(vx,0,sizeof(vx));
            memset(vy,0,sizeof(vy));
            if(find(i))break;
            for(int j=1;j<=n;j++)
            {
                if(vx[j])lx[j]-=low;
            }
            for(int j=1;j<=n;j++)
            {
                if(vy[j])ly[j]+=low;
            }
        }
    }
    ll sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=a[match[i]][i];
    }
    printf("%I64d\n",(ll)n*(ll)0x3f3f3f3f-sum);
}
int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&kk,&pp))
    {
        memset(map,0x3f3f3f3f,sizeof(map));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&at[i]);
        }
        for(int i=1;i<=kk;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            if(w<map[x][y])
            {
                map[x][y]=w;
                map[y][x]=w;
            }
        }
        for(int i=1;i<=pp;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            if(map[y][x+m]>w)
            {
                map[y][x+m]=w;
            }
        }
        for(int i=1;i<=n+m;i++)
        {
            for(int j=1;j<=n+m;j++)
            {
                for(int k=1;k<=n+m;k++)
                {
                    map[j][k]=min(map[j][i]+map[i][k],map[j][k]);
                }
            }
        }
        KM();
    }
}




版权声明:0.0小白有写的不好不对的地方请指出~

hdu 2448 Mining Station on the Sea【费用流】本代码思路仅供参考

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja...
  • mengxiang000000
  • mengxiang000000
  • 2016年08月28日 14:53
  • 267

HDU 2448 最短路+KM匹配

先求出每只船到每个港口的最短路,然后j 代码:
  • zcw1993912
  • zcw1993912
  • 2014年07月25日 15:42
  • 298

hdu1317 负权最短路(Floyd+bellman)

首先要读懂题,做这个题时读题看样例琢磨了半天才明白= =; 题目的意思是,一个人从一号房间走到最后一个房间,每个房间有相应的能量值(可正可负),当走到这个房间的时候可以获取这个房间的能量值,如果走到...
  • zs120197
  • zs120197
  • 2016年07月24日 12:45
  • 257

2017多校训练赛第四场 HDU 6071(最短路)

比赛的时候就是蠢……         当时只剩下十几分钟的时候看到这题的……其实很简单的一道最短路,前提是想到了……         题目要求是只有四个点,然后连边成正方形,问从2号点出发,再回到2号...
  • u013534123
  • u013534123
  • 2017年08月03日 22:44
  • 1008

ACM-最短路(SPFA,Dijkstra,Floyd)之最短路——hdu2544

ACM 最短路(SPFA,Dijkstra,Floyd) 最短路 hdu2544
  • lx417147512
  • lx417147512
  • 2014年05月27日 11:59
  • 3359

HDU-1584-蜘蛛牌

HDU-1584-蜘蛛牌
  • loy_184548
  • loy_184548
  • 2016年04月01日 12:22
  • 318

hdu 3790 最短路径问题(多权值的最短路)

#include #include #include using namespace std; const int inf=100000000; struct In { int x, y; }...
  • u013365671
  • u013365671
  • 2014年08月13日 19:55
  • 526

HDU:最短路(单源最短路径Dijistra)

最短路Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis...
  • zugofn
  • zugofn
  • 2016年04月07日 17:52
  • 385

hdu oj 2544 最短路(最短路径)

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm...
  • u014253173
  • u014253173
  • 2014年08月10日 15:16
  • 1747

HDU 3790 最短路问题(最短路)

【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=3790题目意思如题,就多问了个花费而已解题思路创个结构体跑模板就可以了代码部分#include ...
  • pashuihou
  • pashuihou
  • 2017年12月16日 21:37
  • 25
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 2448 Mining Station on the Sea【最短路Floyd+KM 最佳匹配】
举报原因:
原因补充:

(最多只允许输入30个字)