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

原创 2016年08月28日 14:53:15

Mining Station on the Sea

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


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
 

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

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

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

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

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


思路:


1、事实证明,现在的hdu应该是比以前的hdu跑的慢了,我的代码TLE之后,在网上找到了好几个和我思路一模一样的代码,提交上去也是TLE。


2、这个思路就留下来对应学习参考一下,以后学了KM再过来补上这个Ac。

建图如下:

①建立源点S,将其连入各个船所在的点,权值设定为1,费用设定为0,表示只有一个船.

②建立汇点T,将各个港口连入汇点T,权值设定为1,费用设定为0,表示只能停一个船.

③将m条无向边直接建立起来,权值设定为其距离,费用设定为INF。表示可以走无数次。

④将p条有向边直接建立起来,将点连入港口,权值设定为其距离,费用设定为1。


Tle代码:


#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
    int from;
    int to;
    int w;
    int f;
    int next;
    int num;
}e[1515151];
int head[20000];
int dis[20000];
int pre[20000];
int path[20000];
int vis[20000];
int n,m,kk,pp,cont,ss,tt;
void add(int from,int to,int w,int f)
{
    e[cont].w=w;
    e[cont].f=f;
    e[cont].to=to;
    e[cont].num=cont;
    e[cont].next=head[from];
    head[from]=cont++;
}
int SPFA()
{
    for(int i=0;i<=tt;i++)dis[i]=0x3f3f3f3f;
    memset(vis,0,sizeof(vis));
    queue<int >s;
    s.push(ss);
    vis[ss]=1;
    dis[ss]=0;
    while(!s.empty())
    {
        int u=s.front();
        s.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int f=e[i].f;
            int w=e[i].w;
            if(f&&dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                pre[v]=u;
                path[v]=e[i].num;
                if(vis[v]==0)
                {
                    s.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    if(dis[tt]!=0x3f3f3f3f)return 1;
    else return 0;
}
void Mincost_Maxflow()
{
    int ans=0;
    int maxflow=0;
    while(SPFA()==1)
    {
        int minn=0x3f3f3f3f;
        for(int i=tt;i!=ss;i=pre[i])
        {
            minn=min(minn,e[path[i]].f);
        }
        for(int i=tt;i!=ss;i=pre[i])
        {
            e[path[i]].f-=minn;
            e[path[i]^1].f+=minn;
        }
        ans+=dis[tt]*minn;
        maxflow+=minn;
    }
    printf("%d\n",ans);
}
int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&kk,&pp))
    {
        ss=n+m+1;
        tt=ss+1;
        cont=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++)
        {
            int pos;
            scanf("%d",&pos);
            add(ss,pos,0,1);
            add(pos,ss,0,0);
        }
        for(int i=0;i<kk;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            add(x,y,w,INF);
            add(y,x,-w,0);
            add(y,x,w,INF);
            add(x,y,-w,0);
        }
        for(int i=0;i<pp;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            add(y,x+m,w,1);
            add(x+m,y,-w,0);
        }
        for(int i=1;i<=n;i++)
        {
            add(i+m,tt,0,1);
            add(tt,i+m,0,0);
        }
        Mincost_Maxflow();
    }
}






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

ACM hdu5352 最小费用最大流 模板 网络流

先建图,求字典序最小,转化成前面的费用较大,的费用流 for (int i=0; i 2015多校第5场-hdu5352 起初用ISAP最大流WA了,再后来用了费用流模板TLE,最后使用了大白书...
  • nickwong_
  • nickwong_
  • 2015年08月04日 20:44
  • 1613

hdu3435 费用流

A new Graph Game Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others...
  • JustSteps
  • JustSteps
  • 2013年04月25日 21:57
  • 1229

hdu 3488 Tour(费用流,去重边)

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Sub...
  • acm_cxq
  • acm_cxq
  • 2016年08月02日 16:27
  • 283

Matrix (hdu 2686 最大费用最大流)

题意:给出一个n*n的矩阵,每个点上都有一个值,现在从左上角沿着一条路径走到右下脚(只能向右或者向下),然后再从右下角回到左上角(只能向左或者向上),在这个过程中每个点只允许走一次,问路径上的权值之和...
  • u014422052
  • u014422052
  • 2015年07月05日 14:14
  • 431

最小费用最大流(讲解+模板)

         问题引入:最小费用最大流问题是经济学和管理学中的一类典型问题。在一个网络中每段路径都有“容量”和“费用”两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择路径、...
  • haut_ykc
  • haut_ykc
  • 2016年08月07日 16:40
  • 4253

最小费用最大流代码模板及注释

网络流 最小费用最大流模板和注释
  • tengfei461807914
  • tengfei461807914
  • 2017年01月28日 12:52
  • 1049

hdu4862Jump【最大费用最大流,判断是否满流】2014多校联合

Jump Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub...
  • zhou_yujia
  • zhou_yujia
  • 2016年06月24日 17:04
  • 457

网络流之费用流

求费用流目前好像只有EK+SPFA改版,时间复杂度为O(N*E*k),其中K为最大流值。但时间上的期望时间复杂度为:O(A*E*K),其中A为所有顶点进队列的平均次数,可以证明A一般小于等于2。 ...
  • yo_bc
  • yo_bc
  • 2017年06月06日 12:57
  • 261

HDU 4940 无源无汇上下界最大流

对于每条有向边lai's
  • ipqhjjybj
  • ipqhjjybj
  • 2014年08月13日 12:21
  • 1298

网络流模板(费用流/可行流/上下界)

终于来写博客了= = 一般的最大流 算法:dinic          博主只会这个 大概就是先bfs分层,在增广的时候只走跨层边 加当前弧优化好像快10倍左右,加的时候小心点,容易错 应该只有我...
  • zawedx
  • zawedx
  • 2017年04月07日 15:53
  • 491
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 2448 Mining Station on the Sea【费用流】本代码思路仅供参考
举报原因:
原因补充:

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