HDU 1839 Delay Constrained Maximum Capacity Path(最短路+二分搜索)
Delay Constrained Maximum Capacity Path
Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1856 Accepted Submission(s): 611
Problem Description
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.
Input
The
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.
Output
For
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
1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <queue> 5 #include <algorithm> 6 #define N 10005 7 #define M 50005 8 #define inf 0X3f3f3f3f 9 using namespace std; 10 struct node{ 11 int next,num,time; 12 }; 13 vector<node> v[N]; 14 int n,m,T,limit; 15 int num[M]; 16 int len[N]; 17 bool vis[N]; 18 void add(int a,int b,int c,int d){ 19 node aa; 20 aa.next=b; 21 aa.num=c; 22 aa.time=d; 23 v[a].push_back(aa); 24 aa.next=a; 25 v[b].push_back(aa); 26 } 27 int Dijkstra(int start){ 28 node a; 29 memset(vis,false,sizeof(vis)); 30 memset(len,inf,sizeof(len)); 31 len[start]=0; 32 queue<int> q; 33 q.push(start); 34 while(!q.empty()){ 35 start=q.front(); 36 q.pop(); 37 vis[start]=false; 38 for(int i=0;i<v[start].size();i++){ 39 node a=v[start][i]; 40 if(a.num>=limit){ 41 int ans=len[start]+a.time; 42 if(len[a.next]>ans){ 43 len[a.next]=ans; 44 if(!vis[a.next]){ 45 vis[a.next]=true; 46 q.push(a.next); 47 } 48 } 49 } 50 } 51 } 52 return len[n]; 53 } 54 int main(){ 55 cin.sync_with_stdio(false); 56 int tt,a,b,c,d; 57 cin>>tt; 58 while(tt--){ 59 cin>>n>>m>>T; 60 for(int i=1;i<=n;i++){ 61 v[i].clear(); 62 } 63 for(int i=0;i<m;i++){ 64 cin>>a>>b>>c>>d; 65 add(a,b,c,d); 66 add(b,a,c,d); 67 num[i]=c; 68 } 69 sort(num,num+m,greater<int>()); 70 int l=0,r=m-1; 71 while(l<r){ 72 int mid=(l+r)>>1; 73 limit=num[mid]; 74 int ans=Dijkstra(1); 75 if(ans==inf||ans>T){ 76 l=mid+1; 77 } 78 else{ 79 r=mid; 80 } 81 } 82 cout<<num[l]<<endl; 83 } 84 return 0; 85 }
2016-12-19 13:16:25