首页 > 其他 > 详细

uva 10594 Data Flow(最小费用流)

时间:2014-04-18 07:31:17      阅读:426      评论:0      收藏:0      [点我收藏+]

 

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.

 

bubuko.com,布布扣

 

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 ≤ ≤ 100), M (1 ≤ ≤ 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.

 

Problemsetter: Md. Kamruzzaman

Member of Elite Problemsetters‘ Panel


题目大意:

运输数据从1到n,每条路的花费告诉你,以及路线的流量限定告诉你,问你最小花费


解题思路:

是一题裸的最小费用流,只不过是双向的,应该拆边。


解题代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn=110;
long long inf=1e18;
struct edge{
	int u,v,next,f;
	long long c;
	edge(int u0=0,int v0=0,int f0=0,long long c0=0,int next0=0){
		u=u0,v=v0,f=f0,c=c0,next=next0;
	}
}e[maxn*maxn*10];
int head[maxn*2],visited[maxn*2],path[maxn*2];
long long dist[maxn*2];
int cnt,from,to,marked,n,m,maxc;

void initial(){
	cnt=0;marked=1;
	from=1;to=n;
	memset(head,-1,sizeof(head));
	memset(visited,0,sizeof(visited));
}

void adde(int u,int v,int f,long long c){
	e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].c=c,e[cnt].next=head[u],head[u]=cnt++;
	e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].c=-c,e[cnt].next=head[v],head[v]=cnt++;
}

void input(){
    int u,v,f;
    long long c;
	for(int i=0;i<m;i++){
        scanf("%d%d%lld",&u,&v,&c);
        adde(u,v,0,c);
        adde(v,u,0,c);
	}
	scanf("%d%d",&maxc,&f);
	for(int i=0;i<cnt;i+=2){
        e[i].f=f;
	}
}

void bfs(){
    for(int i=0;i<=to;i++){
        dist[i]=inf;
	 	path[i]=-1;
    }
    dist[from]=0;
    queue <int> q;
    q.push(from);
    marked++;
    visited[from]=marked;
    while(!q.empty()){
        int s=q.front();
        q.pop();
        for(int i=head[s];i!=-1;i=e[i].next){
            int d=e[i].v;
            if(e[i].f>0 && dist[s]+e[i].c<dist[d]){
                dist[d]=dist[s]+e[i].c;
                path[d]=i;
                if(visited[d]!=marked){
                    visited[d]=marked;
                 	q.push(d);
                }
            }
        }
        visited[s]=-1;
    }
}

void solve(){
	long long ans=0;
	int flow=0;
	while(flow<maxc){
    	bfs();
    	if(path[to]==-1) break;
		int off=(1<<30);
		for(int i=to;i!=from;i=e[path[i]].u){
     		if( e[path[i]].f<off ) off=e[path[i]].f;
    	}
    	if(maxc-flow<off) off=maxc-flow;
		for(int i=to;i!=from;i=e[path[i]].u){
     		e[path[i]].f-=off;
        	e[path[i]^1].f+=off;
    	}
    	ans+=dist[to]*off;
    	flow+=off;
    }
	if(flow<maxc) cout<<"Impossible."<<endl;
	else cout<<ans<<endl;
}

int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		initial();
		input();
		solve();
	}
	return 0;
}





uva 10594 Data Flow(最小费用流),布布扣,bubuko.com

uva 10594 Data Flow(最小费用流)

原文:http://blog.csdn.net/a1061747415/article/details/23970251

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!