hdu 1839 最短路+二分

 

Delay Constrained Maximum Capacity Path

Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 560    Accepted Submission(s): 197


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

 

题意:有n个地点,m条边,有一种矿物在1,矿物加工场在n,假设从1到n运送矿物的时间大于T的话,矿物就会分解,要求运送尽可能多的矿物到加工厂。

思路:二分答案,判断每一次跑一次最短路,判断能否运送到n点。

代码:

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
#include<list>
#include<set>
#include<queue>
#include<stack>
using namespace std;
const int inf=1000000;
const int maxn=300010;
int dist[maxn],head[maxn],tol,m,n,t;
struct Edge
{
        int to,next,val,cap;
}edge[10*maxn];
void add(int u,int v,int p,int w)
{
        edge[tol].to=v;
        edge[tol].val=w;
        edge[tol].cap=p;
        edge[tol].next=head[u];
        head[u]=tol++;
}
struct Node
{
        int id,dist;
        Node(int a=0,int b=0):id(a),dist(b){}
        bool operator < (const Node &b) const
        {
                return dist>b.dist;
        }
};
int fun(int st,int cap)
{
        int i,j,u,v;
        priority_queue<Node> q;
        for(i=1;i<=n;i++)dist[i]=inf;dist[st]=0;
        q.push(Node(st,0));
        while(!q.empty())
        {
                Node ret=q.top();q.pop();
                u=ret.id;
                if(dist[u]>t||ret.dist>t)return 0;
                if(u==n)return 1;
                if(dist[u]<ret.dist)continue;
                for(i=head[u];i!=-1;i=edge[i].next)
                if(cap<=edge[i].cap)
                {
                        v=edge[i].to;
                        if(dist[v]>dist[u]+edge[i].val)
                        {
                                dist[v]=dist[u]+edge[i].val;
                                q.push(Node(v,dist[v]));
                        }
                }
        }
        return 0;
}
int main()
{
        int i,j,k,T,p,q;
        scanf("%d",&T);
        while(T--)
        {
               scanf("%d%d%d",&n,&m,&t);
               memset(head,-1,sizeof(head));tol=0;
               int left=inf,right=-inf,mid;
               while(m--)
               {
                       scanf("%d%d%d%d",&i,&j,&k,&p);
                       add(i,j,k,p);
                       add(j,i,k,p);
                       if(left>k)left=k;
                       if(right<k)right=k;
               }
               while(left<right)
               {
                       mid=(left+right+1)>>1;
                       if(fun(1,mid))left=mid;
                       else right=mid-1;
               }
               printf("%d\n",left);
        }
        return 0;
}

 

posted @ 2013-09-16 20:59  线性无关  阅读(113)  评论(0编辑  收藏  举报