UVA 10594 - Data Flow(费用流)

原创 2013年12月03日 21:25:22

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 ≤ ≤ 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.


题意:给定n个点,m条边和运输时间,给定要运的货物D和边的容量K求运送最少时间。

思路:最大流最小费用。。模版题。源点与1连容量为D,汇点为n。

代码:

#include <stdio.h>
#include <string.h>
#define INFF 0x3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f
#include <queue>
using namespace std;
const int N = 105, M = 50005;

long long value[M], cost[M];
int n, m, A[M], B[M], D, K, E, first[N], next[M], u[M], v[M], pe[N], pv[N], a[N], f[M], w[M], s, t;
queue<int>q;

void add(int a, int b, int value, long long time) {
    u[E] = a; v[E] = b; w[E] = value; cost[E] = time;
    next[E] = first[u[E]];
    first[u[E]] = E ++;
    u[E] = b; v[E] = a; w[E] = 0; cost[E] = -time;
    next[E] = first[u[E]];
    first[u[E]] = E ++;
}

void init() {
    E = s = 0, t = n;
    memset(first, -1, sizeof(first));
    for (int i = 0; i < m; i ++)
	scanf("%d%d%lld", &A[i], &B[i], &value[i]);
    scanf("%d%d", &D, &K);
    for (int i = 0; i < m; i ++) {
	add(A[i], B[i], K, value[i]);
	add(B[i], A[i], K, value[i]);
    }
    add(s, 1, D, 0);
}

void solve() {
    init();
    bool vis[N];
    long long d[N], C = 0;
    int F = 0;
    memset(f, 0, sizeof(f));
    while(1) {
	for (int i = 0; i <= n; i ++)
	    d[i] = INFF;
	d[s] = 0;
	memset(vis, 0, sizeof(vis));
	q.push(s);
	while(!q.empty()) {
	    int u = q.front(); q.pop();
	    vis[u] = 0;
	    for (int e = first[u]; e != -1; e = next[e]) {
		if (w[e] > f[e] && d[v[e]] > d[u] + cost[e]) {
		    d[v[e]] = d[u] + cost[e];
		    pv[v[e]] = u; pe[v[e]] = e;
		    if (!vis[v[e]]) {
			vis[v[e]] = 1;
			q.push(v[e]);
		    }
		}
	    }
	}
	if (d[t] == INFF) break;
	int a = INF;
	for (int v = t; v != s; v = pv[v])
	    a = min(a, w[pe[v]] - f[pe[v]]);
	for (int v = t; v != s; v = pv[v]) {
	    f[pe[v]] += a;
	    f[pe[v]^1] -= a;
	}
	C += d[t] * a;
	F += a;
    }
    if (F < D) printf("Impossible.\n");
    else printf("%lld\n", C);
}

int main() {
    while (~scanf("%d%d", &n, &m)) {
	solve();
    }
    return 0;
}



相关文章推荐

UVa 10594 Data Flow (最小费用最大流+有问题)

题意:给出一张图,然后给出K和D,D表示每条路的容量,然后由点1到点n运送数据,流量为K时的最小费用。 #include #include #include #include #includ...

uva 10594 - Data Flow(最小费用最大流)

Problem F Data Flow Time Limit 5 Seconds   In the latest Lab of IIUC, i...

UVA - 10594 Data Flow (最小费用最大流)

题目大意: 在无向图G中把D单位的数据从1号点传到N号点,每条边允许传输数据的最大容量均为K,给出每条边传输单位数据的费用,求最小传输D单位数据的费用。 解析: 注意是无向图,必然要用邻接表...

UVa10594 Data Flow

题意:网络中传送数据量为D的数据,每条边容量为K,

无向图最小费用最大流+uva10594

思路:无向图,加边时需要两个方向,然后加一个超级源点0,0到1的容量为D,然后求最小费用最大流,如果=D,则输出最小费用,否则Impossible.注意数据的范围,要用long long。 附代...

uva10594(最小费用最大流)

题目的意思就 #include #include #include #define ll long long using namespace std; const ll INF ...

uva 10594 Data Flow

uva 10594 Data Flow

uva 10594(最大流最小费)

题意: 题解:

Poj 2195 Going Home【费用流Min_Cost_Max_flow】

Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21528   Ac...

hrbust 1716 双狙人【费用流Min_Cost_Max_flow】

双狙人 Time Limit: 1000 MS Memory Limit: 32768 K   Total Submit: 27(13 users) Total...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA 10594 - Data Flow(费用流)
举报原因:
原因补充:

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