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. |
题意:在一个给定的图里面问流量D的最小费用是多少。
思路:费用流模板题,注意结果要用longlong
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
struct node
{
int v,cost,flow,next;
}edge[20010];
int n,m,tot,F,F2,pre[110],Head[110],f[110],dis[110],INF=1e9;
ll C;
bool vis[110];
queue<int> qu;
void add(int u,int v,int cost)
{
edge[tot].v=v;
edge[tot].cost=cost;
edge[tot].next=Head[u];
Head[u]=tot++;
}
int spfa()
{
int i,j,k,u,v,p;
memset(vis,0,sizeof(vis));
memset(f,0,sizeof(f));
memset(pre,-1,sizeof(pre));
for(i=1;i<=n;i++)
dis[i]=INF;
dis[1]=0;
vis[1]=1;
qu.push(1);
f[1]=F2-F;
while(!qu.empty())
{
u=qu.front();
vis[u]=0;
qu.pop();
for(p=Head[u];p>=0;p=edge[p].next)
{
v=edge[p].v;
if(edge[p].flow>0 && dis[v]>dis[u]+edge[p].cost)
{
dis[v]=dis[u]+edge[p].cost;
f[v]=min(f[u],edge[p].flow);
pre[v]=p;
if(!vis[v])
{
vis[v]=1;
qu.push(v);
}
}
}
}
if(dis[n]==INF)
return 0;
F+=f[n];
C+=(ll)f[n]*dis[n];
if(F==F2)
return 0;
for(p=pre[n];p>=0;p=pre[edge[p^1].v])
{
edge[p].flow-=f[n];
edge[p^1].flow+=f[n];
}
return 1;
}
int main()
{
int i,j,k,u,v,cost,flow;
while(~scanf("%d%d",&n,&m))
{
memset(Head,-1,sizeof(Head));
tot=0;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&cost);
add(u,v,cost);
add(v,u,-cost);
add(v,u,cost);
add(u,v,-cost);
}
scanf("%d%d",&F2,&flow);
for(i=0;i<tot;i+=2)
{
edge[i].flow=flow;
edge[i+1].flow=0;
}
F=C=0;
while(spfa()){}
if(F!=F2)
printf("Impossible.\n");
else
printf("%lld\n",C);
}
}

本文探讨了在有限网络带宽条件下,如何高效地发送大量数据,并通过SPFA算法求解最小时间路径问题,实现从本地服务器到最终目标服务器的最短传输时间。
1596

被折叠的 条评论
为什么被折叠?



