Gym - 101490E Charles in Charge【最短路+二分答案】

57 篇文章 0 订阅
26 篇文章 0 订阅

题目链接:https://vjudge.net/problem/Gym-101490E
题意:给你一个图,首先让你求一个最短路s,给你一个x,让你找一条新的路径,这条路径的总长度不能超过s*(1+x%),且这条路径上的所有边权的最大值最小
解析:先跑一次最短路,这是没毛病的,那么接下来怎么求解第二个问题呢,如果告诉你这条路径的阈值,让你找一条路径,肯定是可以找的,那么就可以通过二分答案来找这条路径,如果这条路径的总和小于要求,那么r=mid,这样做下去就可以出来了

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x7fffffff;
const int maxn = 1e5+100;
typedef long long ll;
struct node
{
    int to,c;
    node() {}
    node(int _to,int _c)
    {
        to = _to;
        c = _c;
    }
    bool operator < (const node &b)const
    {
        return c>b.c;
    }
};
vector<node> G[maxn];
ll d[maxn];
ll dj(ll mid,int s,int n)
{
    priority_queue<node>q;
    fill(d+1,d+n+1,inf);
    d[s] = 0;
    q.push(node(s,0));
    while(!q.empty())
    {
        node now = q.top();
        q.pop();
        int u = now.to;
        if(d[u]<now.c)
            continue;
        for(int i=0;i<(int)G[u].size();i++)
        {
            node v = G[u][i];
            if(d[v.to]>d[u]+v.c && v.c<=mid)
            {
                d[v.to] = d[u]+v.c;
                q.push(node(v.to,d[v.to]));
            }
        }
    }
    return d[n];
}
int main(void)
{
    int n,m,x;
    scanf("%d %d %d",&n,&m,&x);
    for(int i=0;i<m;i++)
    {
        int x,y,c;
        scanf("%d %d %d",&x,&y,&c);
        G[x].push_back(node(y,c));
    }
    ll l = 1,r = 1e9;
    ll res = dj(inf,1,n);
    ll ans = inf;
    res = res*(100+x);
    for(int i=0;i<200;i++)
    {
        ll mid = (l+r)/2;
        ll tmp = dj(mid,1,n);
        tmp = tmp*100LL;
        if(tmp<=res)
        {
            ans = min(mid,ans);
            r = mid;
        }
        else
            l = mid;
    }
    printf("%I64d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值