Mining Station on the Sea (hdu 2448 SPFA+KM)

原创 2015年07月10日 16:53:51

Mining Station on the Sea

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


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 s2448eabed 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
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2454 2452 2451 2447 2453 
 


题意:有m个海上基站,n个港湾,现在有n只船在n个基站里,基站与基站之间有通讯的船才可以走这条路,告诉基站之间的距离,基站与港湾的距离,现在船要回到港湾,一个港湾只能停靠一只船,而且一旦进去就不能出来了,求所有船都回到港湾要走的最短距离之和。

思路:先用最短路求出每个船的起始点到每个港湾的最短距离,并且连边,然后求二分图的最小权匹配,用KM算法。费用流也可以做,但我姿势不够优美超时了。。。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

const int N=350;
const int MAXM = 1000000;

struct Edge{
    int u,v,len,next;
}edge[MAXM];

int n,m,k,p,num;
int dis[N],head[N];
bool inq[N];

int nx,ny;      //两边的点数
int g[N][N];    //二分图描述,g赋初值为-INF
int linker[N],lx[N],ly[N];  //y 中各点匹配状态,x,y中的点的标号
int slack[N];
bool visx[N],visy[N];
bool flag;

void init()
{
    num=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int len)
{
    edge[num]={u,v,len,head[u]};
    head[u]=num++;
}

void SPFA(int s)
{
    int i,j;
    queue<int>Q;
    memset(inq,false,sizeof(inq));
    memset(dis,INF,sizeof(dis));
    Q.push(s);
    dis[s]=0;
    inq[s]=true;
    while (!Q.empty())
    {
        int u=Q.front();Q.pop();
        inq[u]=false;
        for (int i=head[u];i+1;i=edge[i].next)
        {
            int v=edge[i].v;
            if (dis[v]>dis[u]+edge[i].len)
            {
                dis[v]=dis[u]+edge[i].len;
                if (!inq[v])
                {
                    Q.push(v);
                    inq[v]=true;
                }
            }
        }
    }
}

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()
{
    flag=true;
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));
    for (int i=0;i<nx;i++) //赋初值,lx置为最大值
    {
        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||g[linker[i]][i]<=-INF) //有的点不能匹配的话return-1
        {
            flag=false;
            continue;
        }
        res+=g[linker[i]][i];
    }
    return res;
}
//记得nx和ny初始化!!!!!!!!

int start[N];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
#endif
    int i,j,u,v,cost;
    while (~scanf("%d%d%d%d",&n,&m,&k,&p))
    {
        init();
        nx=n;
        ny=n;
        for (i=0;i<n+m+10;i++)
            for (j=0;j<n+m+10;j++)
                g[i][j]=-INF;
        for (i=1;i<=n;i++)
            sf(start[i]);
        for (i=0;i<k;i++)
        {
            sfff(u,v,cost);
            addedge(u,v,cost);      //站点之间的便可以走多次
            addedge(v,u,cost);
        }
        for (i=0;i<p;i++)
        {
            sfff(u,v,cost);
            addedge(v,u+m,cost);      //注意这里是单向边,因为港口只进不出
        }
        for (i=1;i<=n;i++)
        {
            SPFA(start[i]);         //SPFA求出每个船起始位置到港湾的最短距离
            for (j=1;j<=n;j++)
            {
                if (dis[j+m]!=INF)
                    g[i-1][j-1]=-dis[j+m];
//                else
//                    g[i-1][j-1]=0;
            }
        }
        int ans=KM();
        printf("%d\n",-ans);
    }
    return 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
  • 270

SEA 参数解释 和 SEA failover 测试

PowerVM 在虚拟化中用的越来越多,看到 SEA 的重要性了。
  • shenghuiping2001
  • shenghuiping2001
  • 2015年05月26日 15:42
  • 1678

频繁模式挖掘(Frequent Pattern Mining)

频繁模式挖掘()
  • OrthocenterChocolate
  • OrthocenterChocolate
  • 2014年09月28日 09:45
  • 10014

HDU 2448 最短路+KM匹配

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

【Machine Learning】【Python】四、Hard Negative Mining优化训练SVM模型 ---- 《SVM物体分类和定位检测》

前言 通过PSO优化参数,训练好的模型准确率提升了4%左右。还不满足预期目标。所以又尝试通过hard negative mining优化训练SVM模型。原理大概就是先用原始训练集训练出一个模型,然后对...
  • renhanchi
  • renhanchi
  • 2017年06月26日 09:01
  • 495

【强烈推荐】:关于系统学习数据挖掘(Data Mining)的一些建议!!

微信公众号 关键字全网搜索最新排名 【机器学习算法】:排名第一 【机器学习】:排名第一 【Python】:排名第三 【算法】:排名第四 关于数据挖掘 提到收据挖掘(Data Mining...
  • Mbx8X9u
  • Mbx8X9u
  • 2017年12月03日 00:00
  • 116

Machine Learning and Data Mining——2. 机器学习学习路线与资料

机器学习研究的是计算机怎样模拟人类的学习行为,以获取新的知识或技能,并重新组织已有的知识结构使之不断改善自身。简单一点说,就是计算机从数据中学习出规律和模式,以应用在新数据上做预测的任务。本文为转载文...
  • u010757264
  • u010757264
  • 2016年03月05日 10:24
  • 1079

DataMining学习2_数据挖掘十大经典算法

数据挖掘十大经典算法  一、 C4.5  C4.5算法是机器学习算法中的一种分类决策树算法,其核心算法是ID3 算法.   C4.5算法继承了ID3算法的优点,并在以下几方面对ID3算法进行了...
  • wang_zhenwei
  • wang_zhenwei
  • 2016年09月14日 10:26
  • 1419

Wlan_station例程测试

Wlan_station示例程序在下载的SDK软件包example\getting_started_with_wlan_ststion目录中,如果对于CCS使用还不熟练可以尝试按照博主的方法导入工程。...
  • Yusanduan
  • Yusanduan
  • 2015年08月14日 10:34
  • 748

MLAPP学习笔记-Data Mining和Machine Learning的区别及延伸

MLAPP学习笔记-Data Mining和Machine Learning的区别及延伸 一、写在前面   从上学开始,都习惯把笔记记录在纸张上,大多数是觉得可以偶尔练练字什么的。...
  • zwl_123
  • zwl_123
  • 2016年07月25日 14:49
  • 1613
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Mining Station on the Sea (hdu 2448 SPFA+KM)
举报原因:
原因补充:

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