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. |
Problemsetter: Md. Kamruzzaman
Member of Elite Problemsetters‘ Panel
题意:
在无向图G中把D单位的数据从1号点传到N号点,每条边允许传输数据的最大容量均为K,给出每条边传输单位数据的费用,求最小传输D单位数据的费用。
分析:
注意是无向图,必然要用邻接表,将无向边分成两个方向的有向边。1为源点,N为汇点,求流量为D的最小费用,用SPFA找增广路。
#include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #include<ctime> #include<cctype> #include<cmath> #include<string> #include<cstring> #include<stack> #include<queue> #include<list> #include<vector> #include<map> #include<set> #define sqr(x) ((x)*(x)) #define LL long long #define itn int #define INF 0x3f3f3f3f #define INF_LL 0x3f3f3f3f3f3f3f3fLL #define PI 3.1415926535897932384626 #define eps 1e-10 #define maxm 10007<<2 #define maxn 107 using namespace std; int fir[maxn],p[maxn]; long long flow[maxm],cost[maxm],cap[maxm]; int u[maxm],v[maxm],rev[maxm],nex[maxm]; int q[maxm]; bool inq[maxm]; long long d[maxn]; int e_max; long long D,K; void SPFA(int s) { memset(d,0x3f,sizeof d); memset(inq,0,sizeof inq); d[s]=0; int f,r; q[f=r=0]=s; while (f<=r) { int x=q[f++]; inq[x]=false; for (int e=fir[x];~e;e=nex[e]) { if (cap[e]>flow[e] && d[v[e]]>d[u[e]]+cost[e]) { d[v[e]]=d[u[e]]+cost[e]; p[v[e]]=e; if (!inq[v[e]]) { inq[v[e]]=true; q[++r]=v[e]; } } } } } int main() { #ifndef ONLINE_JUDGE freopen("/home/fcbruce/文档/code/t","r",stdin); #endif // ONLINE_JUDGE int n,m; while (~scanf("%d %d",&n,&m)) { e_max=0; memset(fir,-1,sizeof fir); for (int i=0;i<m;i++) { int e=e_max++; scanf("%d %d %lld",&u[e],&v[e],&cost[e]); nex[e]=fir[u[e]];fir[u[e]]=e;rev[e]=e+1; e=e_max++; u[e]=v[e-1];v[e]=u[e-1];cost[e]=-cost[e-1]; nex[e]=fir[u[e]];fir[u[e]]=e;rev[e]=e-1; e=e_max++; u[e]=u[e-1];v[e]=v[e-1];cost[e]=-cost[e-1]; nex[e]=fir[u[e]];fir[u[e]]=e;rev[e]=e+1; e=e_max++; u[e]=v[e-1];v[e]=u[e-1];cost[e]=-cost[e-1]; nex[e]=fir[u[e]];fir[u[e]]=e;rev[e]=e-1; } scanf("%lld %lld",&D,&K); for (int i=0;i<e_max;i++) { if (i&1) cap[i]=0; else cap[i]=K; } memset(flow,0,sizeof flow); LL total_flow=0,total_cost=0; int s=1,t=n; for (;;) { SPFA(s); if (d[t]==INF_LL) break; long long _f=INF_LL; for(int e=p[t];;e=p[u[e]]) { _f=min(_f,cap[e]-flow[e]); if (u[e]==s)break; } for(int e=p[t];;e=p[u[e]]) { flow[e]+=_f; flow[rev[e]]-=_f; if (u[e]==s)break; } if (total_flow+_f<D) { total_flow+=_f; total_cost+=d[t]*_f; } else { total_cost+=d[t]*(D-total_flow); total_flow=D; break; } } if (total_flow==D) printf("%lld\n",total_cost); else puts("Impossible."); } return 0; }
UVA 10594 Data Flow (最小费用流),布布扣,bubuko.com
原文:http://blog.csdn.net/u012965890/article/details/38170933