HDU 2962 Trucking 二分+最短路

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2962

题意

在一张地图中,一个运货车要从s运货到t,路中每条边有运输时间,有些边有运货限制(理解为路有路有承重限制),有些没有。给定运货车的最大运货量,求s到t能运货尽可能多的情况下,花费时间最少。

思路

二分枚举s->t的路中最小的h,将图中所有小于h的边都删去,跑最短路。

#include<cstdio>
#include<queue>
#include<iostream>
#include<vector>
#include<map>
#include<cstring>
#include<string>
#include<set>
#include<stack>
#include<algorithm>
#define cle(a) memset(a,0,sizeof(a))
#define inf(a) memset(a,0x3f,sizeof(a))
#define ll long long
#define Rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
#define INF2 9223372036854775807ll
const int INF = ( 2e9 ) + 2;
const ll maxn = 1010;
const int maxm = maxn*maxn;
struct edge
{
    int v,h,w,next;
}e[maxm];
struct node
{
    int u,d;
    bool operator < (const node &b)const
    {
        return d>b.d;
    }
};
int head[maxn],d[maxn],vis[maxn];
int tot;
void add(int u,int v,int h,int w)
{
    e[tot].v=v;
    e[tot].h=h;
    e[tot].w=w;
    e[tot].next=head[u];
    head[u]=tot++;

    e[tot].v=u;
    e[tot].h=h;
    e[tot].w=w;
    e[tot].next=head[v];
    head[v]=tot++;
}
int dijkstra(int s,int t,int n,int lim)
{
    for(int i=1;i<=n;i++)d[i]=INF;
    d[s]=0;
    priority_queue<node> q;
    memset(vis,0,sizeof(vis));
    q.push(node{s,0});
    while(!q.empty())
    {
        node temp=q.top();q.pop();
        int u=temp.u;
        if(vis[u])continue;
        vis[u]=1;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            if(e[i].h<lim)continue;
            int v=e[i].v;
            int w=e[i].w;
            if(d[v]>d[u]+w)
            {
                d[v]=d[u]+w;
                q.push(node{v,d[v]});
            }
        }
    }
    return d[t];
}
int main()
{
    int n,m;
    int cas=0;
//  freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        memset(head,-1,sizeof(head));
        tot=0;
        for(int i=0;i<m;i++)
        {
            int u,v,h,w;
            scanf("%d%d%d%d",&u,&v,&h,&w);
            if(h==-1)h=INF;
            add(u,v,h,w);
        }
        int s,t,r;
        scanf("%d%d%d",&s,&t,&r);
        int l=0;
        int ans=INF;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            int temp=dijkstra(s,t,n,mid);
            if(temp!=INF)
            {
                l=mid+1;
                ans=temp;
            }
            else
            r=mid-1;
        }
        if(cas!=0)puts("");
        printf("Case %d:\n",++cas);
        if(ans!=INF)
        {
            printf("maximum height = %d\n",r);
            printf("length of shortest route = %d\n",ans);
        }
        else
        printf("cannot reach destination\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值