首页 > ACM题库 > HDU-杭电 > HDU 1839 Delay Constrained Maximum Capacity Path-分治-[解题报告] C++
2013
12-23

HDU 1839 Delay Constrained Maximum Capacity Path-分治-[解题报告] C++

Delay Constrained Maximum Capacity Path

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1839

思路:先给容量从大到小排序,然后二分,每次都一次spfa一下,判断当前的cost[n]的值。。。

#include<iostream>
 #include<queue>
 #include<vector>
 typedef long long ll;
 const int MAXN=10000+10;
 const ll inf=1e60;
 using namespace std;
 struct Node{
     int v,w,t;
 };
 vector<Node>mp[MAXN];
 ll cost[MAXN];
 ll weight[MAXN*5];
 int n,m,time;
 int limit;
 
 
 int cmp(const ll a,const ll b){
     return a>b;
 }
 
 
 void SPFA(int u){
     for(int i=1;i<=n;i++)cost[i]=inf;
     cost[1]=0;
     queue<int>Q;
     Q.push(u);
     while(!Q.empty()){
         int u=Q.front();
         Q.pop();
         for(int i=0;i<mp[u].size();i++){
             int v=mp[u][i].v;
             int t=mp[u][i].t;
             int w=mp[u][i].w;
             //这儿实在是太妙了,每次选的边都有一个限制就行了
             if(cost[u]+t<cost[v]&&w>=limit){
                 cost[v]=cost[u]+t;
                 Q.push(v);
             }
         }
     }
 }
 
 int main(){
     int _case;
     scanf("%d",&_case);
     while(_case--){
         scanf("%d%d%d",&n,&m,&time);
         for(int i=1;i<=n;i++)mp[i].clear();
         memset(weight,0,sizeof(weight));
         for(int i=1;i<=m;i++){
             int u,v,w,t;
             scanf("%d%d%d%d",&u,&v,&w,&t);
             Node p1,p2;
             p1.v=u,p2.v=v;
             p1.t=p2.t=t;
             p1.w=p2.w=w;
             weight[i]=w;//用来记录每条路的容量;
             mp[u].push_back(p2);
             mp[v].push_back(p1);
         }
         sort(weight+1,weight+m+1,cmp);
         //二分
         int low=1,high=m;
         while(low<=high){
             int mid=(low+high)/2;
             limit=weight[mid];//每次都选择一个限制
             SPFA(1);
             if(cost[n]==inf||cost[n]>time){
                 low=mid+1;
             }else 
                 high=mid-1;
         }
         printf("%d\n",weight[low]);
     }
     return 0;
 }

 

解题报告转自:http://www.cnblogs.com/wally/archive/2013/04/13/3019383.html