hdu 1839 Delay Constrained Maximum Capacity Path【二分+SPFA】

Delay Constrained Maximum Capacity Path

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

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

Author

Mugurel Ionut Andreica

Source

Politehnica University of Bucharest Local Team Contest 2007

 

 题目大意:有n个点,m条无向边,T的时间限制。每条无向边给出四个元素,A,B,C,D分别表示这条路的两个顶点,运送最大容量和运算耗时(边权)。问在时间限制内从节点1到节点n的最大载货量。


思路:


1、对于最大载货量,我们如果从1开始枚举,枚举到最大载货量的话,每一次的枚举都要求一次最短路,时间虽然限制给了10000ms,但是也经不起这么折腾。所以我们优化算法,使用二分匹配枚举最大载货量降低时间复杂度。


2、对于每一次求最短路,对于限制条件满足的同时,如果dis【n】<=T,那么当前载货量就是一个可行载货量,一直二分下去,找到最终解。


3、注意的一个点就是二分查找的过程中,这种情况下的最终mid不一定就是答案,所以我们在二分查找的时候,一定记录的是最后一个可行解,输出这个值,而并非直接输出mid。


AC代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int head[100000];
struct EdgeNode
{
    int to;
    int w;
    int c;
    int next;
}e[1000000];
int dis[100000];
int vis[100000];
int n,m,T,cont,maxn;
void add(int from,int to,int c,int val)
{
    e[cont].to=to;
    e[cont].c=c;
    e[cont].w=val;
    e[cont].next=head[from];
    head[from]=cont++;
}
int SPFA(int mid)
{
    //printf("%d\n",mid);
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)dis[i]=0x7fffffff;
    dis[1]=0;
    vis[1]=1;
    queue<int >s;
    s.push(1);
    while(!s.empty())
    {
        int u=s.front();
        s.pop();vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            int c=e[i].c;
            if(dis[v]>dis[u]+w&&c>=mid)
            {
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    s.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    if(dis[n]<=T)return 1;
    else return 0;
}
void erfen()
{
    int l=0;
    int r=maxn;
    int mid;
    int ans;
    while(r-l>=0)
    {
        mid=(l+r)/2;
        if(SPFA(mid)==1)
        {
            ans=mid;
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    printf("%d\n",ans);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cont=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d%d",&n,&m,&T);
        maxn=0;
        for(int i=0;i<m;i++)
        {
            int x,y,C,val;
            scanf("%d%d%d%d",&x,&y,&C,&val);
            add(x,y,C,val);
            add(y,x,C,val);
            maxn=max(C,maxn);
        }
        erfen();
    }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值