Consider an undirected graph with N vertices, numbered from 1 to N, and M edges. The vertex numbered with 1 corresponds to a mine from where some precious minerals are extracted. The vertex numbered with N corresponds to a minerals processing factory. Each
edge has an associated travel time (in time units) and capacity (in units of minerals). It has been decided that the minerals which are extracted from the mine will be delivered to the factory using a single path. This path should have the highest capacity
possible, in order to be able to transport simultaneously as many units of minerals as possible. The capacity of a path is equal to the smallest capacity of any of its edges. However, the minerals are very sensitive and, once extracted from the mine, they
will start decomposing after T time units, unless they reach the factory within this time interval. Therefore, the total travel time of the chosen path (the sum of the travel times of its edges) should be less or equal to T.
InputThe first line of input contains an integer number X, representing the number of test cases to follow. The first line of each test case contains 3 integer numbers, separated by blanks: N (2 <= N <= 10.000), M (1 <= M <= 50.000) and T (1 <= T <= 500.000).
Each of the next M lines will contain four integer numbers each, separated by blanks: A, B, C and D, meaning that there is an edge between vertices A and B, having capacity C (1 <= C <= 2.000.000.000) and the travel time D (1 <= D <= 50.000). A and B are different
integers between 1 and N. There will exist at most one edge between any two vertices.
OutputFor each of the X test cases, in the order given in the input, print one line containing the highest capacity of a path from the mine to the factory, considering the travel time constraint. There will always exist at least one path between the mine and
the factory obbeying the travel time constraint.
Sample Input
Sample Output
Input
2 2 1 10 1 2 13 10 4 4 20 1 2 1000 15 2 4 999 6 1 3 100 15 3 4 99 4
13 99
给你n个点,从1到n为其标号,现在要你在t时间内从1运东西到n,有m条路A B C D,表示从A到B的运输量为C,时间为D;要求运输量尽可能多,运输量由运输量最小的那条边决定;
用dijkstra超时了,只好改为spfa,初始话时也只能用memset才不超时,并且INF如果是用 1<<28表示时memset没用,只能表达为 0x3f3f3f3f;将各条边的运输量放到新的数组里排序,再二分,在spfa中进行松弛时把边的运输量加入限制条件
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue>#define INF 0x3f3f3f3f using namespace std; struct Edge { int v,next,w,c; }edge[100100]; int n,m,cnt,time; int head[10100],capa[100100]; int vis[10100],d[10100]; ///struct node ///{ /// int pre,id; ///}used[N]; void addedge(int x,int y,int c,int w) { edge[cnt].v=y;edge[cnt].w=w;edge[cnt].c=c; edge[cnt].next=head[x]; head[x]=cnt++; }int spfa(int ca) { memset(d,INF,sizeof(d)); memset(vis,0,sizeof(vis));queue<int>it; vis[1]=1;d[1]=0;it.push(1); while(!it.empty()) { int u=it.front();it.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { if(edge[i].c>=ca&&d[edge[i].v]>d[u]+edge[i].w) { d[edge[i].v]=d[u]+edge[i].w; if(!vis[edge[i].v]) { vis[edge[i].v]=1;it.push(edge[i].v); } } } } return d[n]<=time; } int main() { int t;scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&time); memset(head,-1,sizeof(head)); cnt=0; for(int i=0;i<m;i++) { int x,y,w,c;scanf("%d%d%d%d",&x,&y,&c,&w); addedge(x,y,c,w); addedge(y,x,c,w); capa[i]=c; } sort(capa,capa+m); int low=0,high=m-1;int pre=0; while(high>=low) { int mid=(low+high)>>1; if(spfa(capa[mid])){low=mid+1;pre=mid;} else high=mid-1; } printf("%d\n",capa[pre]); } }

本文介绍了一种改进的最短路径快速算法(SPFA)在特定场景下的应用,即在有限时间内寻找最大容量路径的问题。通过对比Dijkstra算法,解释了为何SPFA更适合解决该问题,并详细展示了算法的具体实现过程。
1399

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



