uva10594 - Data Flow 无向图的最小费用

Problem F

Data Flow

Time Limit

5 Seconds

 

In the latest Lab of IIUC, it requires to send huge amount of data from the local server to the terminal server. The lab setup is not yet ready. It requires to write a router program for the best path of data. The problem is all links of the network has a fixed capacity and cannot flow more than that amount of data. Also it takes certain amount of time to send one unit data through the link. To avoid the collision at a time only one data unit can travel i.e. at any instant more than one unit of data cannot travel parallel through the network. This may be time consuming but it certainly gives no collision. Each node has sufficient buffering capability so that data can be temporarily stored there. IIUC management wants the shortest possible time to send all the data from the local server to the final one.

 

 

For example, in the above network if anyone wants to send 20 unit data from A to D, he will send 10 unit data through AD link and then 10 unit data through AB-BD link which will take 10+70=80 unit time.

 

Input

Each input starts with two positive integers N (2 N 100), M (1 M 5000). In next few lines the link and corresponding propagation time will be given. The links are bidirectional and there will be at most one link between two network nodes. In next line there will be two positive integers D, K where D is the amount of data to be transferred from 1st to N'th node and K is the link capacity. Input is terminated by EOF.

Output

For each dataset, print the minimum possible time in a line to send all the data. If it is not possible to send all the data, print "Impossible.". The time can be as large as 1015.

 

Sample Input

Output for Sample Input

4 5
1 4 1
1 3 3
3 4 4
1 2 2
2 4 5
20 10
4 4
1 3 3
3 4 4
1 2 2
2 4 5
20 100
4 4
1 3 3
3 4 4
1 2 2
2 4 5
20 1
80
140
Impossible

  上次那个dijkstra,dijkstra就是求无向图的最小费用,但是那个是特殊情况,若找到最短增广路,增广的值肯定是1,下次若再经过相同的边,增广值也是1,就相当于把上次经过的流退回去。所以每次把增广路上的cost变成负的就行了。

  但在一般的无向图情况下,就必须要用链接表了(还不能是vector),两个方向分别建边。因为还要建残量边,就相当于两个点之间要建4个边,分2组,每组前一个容量是cap,花费cost,后一个(残量边)容量是0,花费-cost。边的编号前一个是偶数,后一个是奇数,这样更新flow的时候就可以方便的用^。

  还有个区别是在记录路径的时候,不光要p[v]=u,还要多一个数组,设为pe,如果编号为i的边更新了d[v],则pe[v]=i。最后找增广值和更新流量的时候操作的是e[i].flow,e[i].cap(代替里连接矩阵时候的flow[p[u]][u],cap[p[u]][u])。


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#define LLINF 1000000000000100
#define INF 0x3f3f3f3f
#define MAXN 110
#define MAXM 5010
#define eps 1e-9
using namespace std;
int N,M,D,K,id;
int x[MAXM],y[MAXM],head[MAXN],inq[MAXN],p[MAXN],pe[MAXN];
long long cost[MAXM],d[MAXN],ans;

struct edge{
    int v,cap,flow,next;
    long long w;
}e[4*MAXM];

void add_edge(int u,int v,int cap,long long w){
    e[id].next=head[u];
    e[id].v=v;
    e[id].cap=cap;
    e[id].w=w;
    e[id].flow=0;
    head[u]=id++;
}
int min_cost(){
    queue<int> q;
    int f=0,s=0,t=N;
    while(1){
        memset(inq,0,sizeof(inq));
        for(int i=0;i<=N;i++) d[i]=(i==s?0:LLINF);
        d[s]=0;
        q.push(s);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            inq[u]=0;
            for(int i=head[u];i!=-1;i=e[i].next){
                int v=e[i].v;
                if(e[i].cap>e[i].flow&&d[v]>d[u]+e[i].w){
                    d[v]=d[u]+e[i].w;
                    p[v]=u;
                    pe[v]=i;
                    if(!inq[v]){
                        q.push(v);
                        inq[v]=1;
                    }
                }
            }
        }
        if(d[t]==LLINF) break;
        long long a=LLINF;
        for(int u=t;u!=s;u=p[u]){
            int i=pe[u];
            if(e[i].cap-e[i].flow<a) a=e[i].cap-e[i].flow;
        }
        for(int u=t;u!=s;u=p[u]){
            int i=pe[u];
            e[i].flow+=a;
            e[i^1].flow-=a;
        }
        f+=a;
        ans+=d[t]*a;
    }
    return f;
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d%d",&N,&M)!=EOF){
        int i;
        for(i=0;i<M;i++) scanf("%d%d%lld",&x[i],&y[i],&cost[i]);
        scanf("%d%d",&D,&K);
        id=0;
        ans=0;
        memset(head,-1,sizeof(head));
        for(i=0;i<M;i++){
            add_edge(x[i],y[i],K,cost[i]);
            add_edge(y[i],x[i],0,-cost[i]);
            add_edge(y[i],x[i],K,cost[i]);
            add_edge(x[i],y[i],0,-cost[i]);
        }
        add_edge(0,1,D,0);
        add_edge(1,0,0,0);
        if(min_cost()==D) printf("%lld\n",ans);
        else printf("Impossible.\n");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值