Delay Constrained Maximum Capacity Path HDU - 1839
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
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
Sample Output
13 99
题解:枚举路的最小容量,显然最小容量是有序的,当前的容量不能满足时间小于T的话,大于当前的容量就更不可能满足,所以是二分枚举啦
1 #include<queue> 2 #include<cstdio> 3 #include<string> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 typedef pair<int,int> P; 10 11 const int INF=2e9+7; 12 const int maxn=5e4+5; 13 14 struct node{ 15 int to,va,next,time; 16 }e[2*maxn]; 17 18 int n,m,tot,kase,T; 19 int d[maxn],cal[maxn],head[maxn]; 20 21 void Inite(){ 22 tot=0; 23 memset(head,-1,sizeof(head)); 24 } 25 26 void addedge(int u,int v,int w,int time){ 27 e[tot].to=v; 28 e[tot].va=w; 29 e[tot].time=time; 30 e[tot].next=head[u]; 31 head[u]=tot++; 32 } 33 34 bool DJS(int limit){ 35 36 for(int i=1;i<=n;i++) d[i]=INF; 37 d[1]=0; 38 39 priority_queue<P,vector<P>,greater<P> > q; 40 q.push(P(0,1)); 41 42 while(!q.empty()){ 43 P p=q.top(); 44 q.pop(); 45 int v=p.second; 46 if(d[v]<p.first) continue; 47 for(int i=head[v];i!=-1;i=e[i].next){ 48 int u=e[i].to; 49 if(e[i].va<limit) continue; 50 if(d[u]>d[v]+e[i].time){ 51 d[u]=d[v]+e[i].time; 52 q.push(P(d[u],u)); 53 } 54 } 55 } 56 if(d[n]>T) return false; 57 else return true; 58 } 59 60 int main() 61 { cin>>kase; 62 while(kase--){ 63 scanf("%d%d%d",&n,&m,&T); 64 65 Inite(); 66 for(int i=1;i<=m;i++){ 67 int u,v,ca,time; 68 scanf("%d%d%d%d",&u,&v,&ca,&time); 69 cal[i]=ca; 70 addedge(u,v,ca,time); 71 addedge(v,u,ca,time); 72 } 73 74 sort(cal+1,cal+m+1); 75 //感觉这种的二分写法正确率比较高!!!! 76 int l=1,r=m,mid; 77 for(int i=1;i<=100;i++){ 78 mid=(l+r)>>1; 79 if(DJS(cal[mid])) l=mid; 80 else r=mid; 81 } 82 printf("%d\n",cal[l]); 83 } 84 return 0; 85 }

浙公网安备 33010602011771号