【HDU 1839】 Delay Constrained Maximum Capacity Path(二分+最短路)

【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): 1515    Accepted Submission(s): 481

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

Recommend

lcy

一个无向图 每条边有流量跟花费(时间) 要求寻找在花费不超过D的情况下1->n的一条流量最大的路线

路线的流量是指路线中能承受的最大流量 即路线中一条流量最小的路径的流量

给的边50000条 当时没想出来怎么做 只知道时间很长 感觉可以搜 但不知道怎么搜 像是最小费的变形(费用不超过时的单条最大流)

之后知道是二分+最短路 因为边少 单独存一下排个序 然后二分 没二分到一个流量 看一下1->n存不存在流量>=该流量且在时间范围内的路径 然后不断二分+搜

二分要满足单调 排序后流量单调了 然后流量越大 可行路线越少 流量越少可行路就越多 因此是单减的 满足二分条件

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;
const int INF = 0x3f3f3f3f;

struct Edge
{
    int v,c,w,next;
};

//邻接表存图
int head[10100];
Edge eg[100100];
int tp,n,T;
//边流量 排序后用来二分
int cost[100100];
//SPFA
int dis[10100];
bool vis[10100];

void Add(int u,int v,int c,int w)
{
    eg[tp].v = v;
    eg[tp].next = head[u];
    eg[tp].c = c;
    eg[tp].w = w;
    head[u] = tp++;
}

void init()
{
    memset(head,-1,sizeof(head));
    tp = 0;
}

//SPFA 检查是否可行
bool can(int c)
{
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    queue <int> q;
    dis[1] = 0;
    q.push(1);

    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(int i = head[u]; i != -1; i = eg[i].next)
        {
            int v = eg[i].v;
            int cs = eg[i].c;
            int w = eg[i].w;
            if(cs >= c && dis[v] > dis[u]+w)
            {
                dis[v] = dis[u]+w;
                if(!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    //流量满足的情况下 最小花费是否满足限制
    return dis[n] <= T;
}

int main()
{
    int t,m,l,r,ans;
    int u,v,c,w;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d %d %d",&n,&m,&T);
        for(int i = 0; i < m; ++i)
        {
            scanf("%d %d %d %d",&u,&v,&c,&w);
            Add(u,v,c,w);
            Add(v,u,c,w);
            cost[i] = c;
        }

        //排序二分
        sort(cost,cost+m);
        l = 0, r = m-1, ans = -1;
        while(l <= r)
        {
            m = (l+r)>>1;
            //该流量下可行
            if(can(cost[m]))
            {
                ans = m;
                l = m+1;
            }else r = m-1;
        }
        printf("%d\n",cost[ans]);
    }
    return 0;
}
时间: 2024-10-15 17:27:50

【HDU 1839】 Delay Constrained Maximum Capacity Path(二分+最短路)的相关文章

HDU 1839 Delay Constrained Maximum Capacity Path(二分+最短路)

题目地址:HDU 1839 我去..原来这题这么简单...网络流中这种二分建图的方式做了一大堆了..这种题还能难倒我吗...白天一直没怎么看懂题,对题意懵懵懂懂的...晚上好好看了看题,这不就是网络流中练的最多的那种二分建图模型吗....只是把网络流算法改成最短路就行了..但是两个地方手残了没能在实验室当场A掉..sad... 这题就是二分最小容量,对满足容量的加边,对时间求最短路.如果最短时间比规定时间少的话就可以继续增加容量,直到不能增加为止. 代码如下: #include <iostrea

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): 1790    Accepted Submission(s): 577 Problem Description Consider an undirected graph with N vertices, nu

hdu 1839 Delay Constrained Maximum Capacity Path

最短路+二分. 对容量进行二分,因为容量和时间是单调关系的,容量越多,能用的边越少,时间会不变或者增加. 因为直接暴力一个一个容量去算会TLE,所以采用二分. #include<cstdio> #include<vector> #include<cstring> #include<queue> #include<map> #include<algorithm> using namespace std; const int maxn =

hdu1839Delay Constrained Maximum Capacity Path 二分+最短路

//一个无向图,两点之间的流量为c,两点花的时间为t //问从起点到终点n之间时间小于等于T且只走一条路径能够运输的最大流量为多少 //二分流量,小于这个流量的路径不走,求其时间是否小于等于T得到答案 #include<cstdio> #include<cstring> #include<iostream> #include<queue> using namespace std ; const int maxn = 10010 ; const int max

复习图---Delay Constrained Maximum Capacity Path(SPFA+二分)

Delay Constrained Maximum Capacity Path Time Limit:10000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Description Consider an undirected graph with N vertices, numbered from 1 to N, and M edges. The vertex numbered with

HDU1839_Delay Constrained Maximum Capacity Path(最短路+二分)

解题报告 http://blog.csdn.net/juncoder/article/details/38349019 题目传送门 题意: 有N个点,点1为珍贵矿物的采矿区, 点N为加工厂,有M条双向连通的边连接这些点.走每条边的运输容量为C,运送时间为D. 他们要选择一条从1到N的路径运输, 这条路径的运输总时间要在T之内,在这个前提之下,要让这条路径的运输容量尽可能地大. 一条路径的运输容量取决与这条路径中的运输容量最小的那条边. 思路: 二分容量建图,spfa判时间是否符合条件 #incl

HDU1839Delay 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): 1314    Accepted Submission(s): 418 Problem Description Consider an undirected graph with N vertices, n

hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分)

Rabbit's String Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 288    Accepted Submission(s): 108 Problem Description Long long ago, there lived a lot of rabbits in the forest. One day, the

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co