洛谷 P3063 [USACO12DEC]牛奶的路由Milk Routing

题目描述

Farmer John's farm has anoutdated network of M pipes (1 <= M <= 500) for pumping milk from thebarn to his milk storage tank. He wants to remove and update most of these overthe next year, but he wants to leave exactly one path worth of pipes intact, sothat he can still pump milk from the barn to the storage tank. 
The pipe network is described by N junction points (1 <= N<= 500), each of which can serve as the endpoint of a set of pipes. Junctionpoint 1 is the barn, and junction point N is the storage tank. Each of the Mbi-directional pipes runs between a pair of junction points, and has anassociated latency (the amount of time it takes milk to reach one end of thepipe from the other) and capacity (the amount of milk per unit time that can bepumped through the pipe in steady state). Multiple pipes can connect betweenthe same pair of junction points.
 
For a path of pipes connecting from the barn to the tank, thelatency of the path is the sum of the latencies of the pipes along the path,and the capacity of the path is the minimum of the capacities of the pipesalong the path (since this is the "bottleneck" constraining theoverall rate at which milk can be pumped through the path). If FJ wants to senda total of X units of milk through a path of pipes with latency L and capacityC, the time this takes is therefore L + X/C. 
Given the structure of FJ's pipe network, please help himselect a single path from the barn to the storage tank that will allow him topump X units of milk in a minimum amount of total time.

输入

* Line 1: Threespace-separated integers: N M X (1 <= X <= 1,000,000). 
* Lines 2..1+M: Each line describes a pipe using 4 integers: IJ L C. I and J (1 <= I,J <= N) are the junction points at both ends ofthe pipe. L and C (1 <= L,C <= 1,000,000) give the latency and capacityof the pipe.

输出

* Line 1: The minimum amountof time it will take FJ to send milk along a single path, rounded down to thenearest integer.

样例输入 

33 15

1 210 3

3 210 2

1 314 1

样例输出 

27

提示: INPUT DETAILS: FJ wants tosend 15 units of milk through his pipe network. Pipe #1 connects junction point1 (the barn) to junction point 2, and has a latency of 10 and a capacity of 3.Pipes #2 and #3 are similarly defined.

OUTPUTDETAILS: The path 1->3 takes 14 + 15/1 = 29 units of time. The path1->2->3 takes 20 + 15/2 = 27.5 units of time, and is therefore optimal.

题解:我们建好边之后直接暴力枚举最小容量,进行SPFA计算即可。

#include 
#include 
#define N 1005
int n,m,X,xx,yy,l,c,cnt,head,tail;
int first[N],next[N],v[N],w[N],ww[N],dis[N/2];
int q[N*100],minn=0x3f3f3f3f;
bool vis[N/2];
using namespace std;
inline void spfa(int x)
{
	memset(dis,0x3f3f3f3f,sizeof(dis));
	dis[1]=0;
    head=0;
    tail=1;
    q[1]=1;
    vis[1]=1;
    while (headdis[q[head]]+w[i]&&ww[i]>=x)
    		{
    			dis[k]=dis[q[head]]+w[i];
    			if (!vis[k])
    			{
    				tail++;
    				q[tail]=k;
    				vis[k]=1;
    			}
    		}
    	}
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&X);
    for (int i=1;i<=m;i++)
    {
    	scanf("%d%d%d%d",&xx,&yy,&l,&c);
    	next[++cnt]=first[xx];
    	first[xx]=cnt;
    	v[cnt]=yy;
    	w[cnt]=l;
    	ww[cnt]=c;
    	next[++cnt]=first[yy];
    	first[yy]=cnt;
    	v[cnt]=xx;
    	w[cnt]=l;
    	ww[cnt]=c;
    }
    for (int i=1;i<=m;i++)
    {
    	int zz=ww[i*2-1];
		spfa(zz);
		int yy=X/zz;
    	if (dis[n]+yy

你可能感兴趣的:(SPFA)