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

题目地址:HDU 1839

我去。。原来这题这么简单。。。网络流中这种二分建图的方式做了一大堆了。。这种题还能难倒我吗。。。白天一直没怎么看懂题,对题意懵懵懂懂的。。。晚上好好看了看题,这不就是网络流中练的最多的那种二分建图模型吗。。。。只是把网络流算法改成最短路就行了。。但是两个地方手残了没能在实验室当场A掉。。sad。。。

这题就是二分最小容量,对满足容量的加边,对时间求最短路。如果最短时间比规定时间少的话就可以继续增加容量,直到不能增加为止。

代码如下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int head[11000], vis[11000], d[11000], cnt, n;
struct node1
{
    int u, v, w, cost;
}bian[50002];
struct node
{
    int u, v, w, next, cost;
}edge[1000000];
void add(int u, int v, int w, int cost)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].cost=cost;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int spfa()
{
    memset(d,INF,sizeof(d));
    memset(vis,0,sizeof(vis));
    d[1]=0;
    deque<int>q;
    q.push_back(1);
    while(!q.empty())
    {
        int u=q.front();
        q.pop_front();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(d[v]>d[u]+edge[i].cost)
            {
                d[v]=d[u]+edge[i].cost;
                if(!vis[v])
                {
                    vis[v]=1;
                    if(!q.empty()&&d[v]<d[q.front()])
                    {
                        q.push_front(v);
                    }
                    else
                    {
                        q.push_back(v);
                    }
                }
            }
        }
    }
}
int main()
{
    int t, i, m, time, max1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&time);
        max1=-1;
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&bian[i].u,&bian[i].v,&bian[i].w,&bian[i].cost);
            if(max1<bian[i].w)
                max1=bian[i].w;
        }
        __int64 low=0, high=max1, mid, ans=-1;
        while(low<=high)
        {
            mid=(low+high)/2;
            memset(head,-1,sizeof(head));
            cnt=0;
            for(i=0;i<m;i++)
            {
                if(bian[i].w>=mid)
                {
                    add(bian[i].u,bian[i].v,bian[i].w,bian[i].cost);
                    add(bian[i].v,bian[i].u,bian[i].w,bian[i].cost);
                }
            }
            spfa();
            if(d[n]<=time)
            {
                low=mid+1;
                ans=mid;
            }
            else
            {
                high=mid-1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

HDU 1839 Delay Constrained Maximum Capacity Path(二分+最短路),布布扣,bubuko.com

时间: 2024-10-13 11:28:29

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

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

【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

复习图---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