2019西安邀请赛Travel

262144K
There are nn planets in the MOT galaxy, and each planet has a unique number from 1 \sim n1∼n. Each planet is connected to other planets through some transmission channels. There are mm transmission channels in the galaxy. Each transmission channel connects two different planets, and each transmission channel has a length.

The residents of the galaxy complete the interplanetary voyage by spaceship. Each spaceship has a level. The spacecraft can be upgraded several times. It can only be upgraded 11 level each time, and the cost is cc. Each upgrade will increase the transmission distance by dd and the number of transmissions channels by ee to the spacecraft. The spacecraft can only pass through channels that are shorter than or equal to its transmission distance. If the number of transmissions is exhausted, the spacecraft can no longer be used.

Alice initially has a 00-level spacecraft with transmission distance of 00 and transmission number of 00. Alice wants to know how much it costs at least, in order to transfer from planet 11 to planet nn.

Input
Each test file contains a single test case. In each test file:

The first line contains nn, mm, indicating the number of plants and the number of transmission channels

The second line contains cc, dd, ee, representing the cost, the increased transmission distance, and the increased number of transmissions channels of each upgrade, respectively.

Next mm lines, each line contains u,v,wu,v,w, meaning that there is a transmission channel between uu and vv with a length of ww.

(2 \le n\le 10^5, n - 1 \le m \le 10^5,1 \le u,v \le n ,1 \le c,d,e,w \le 10^5)(2≤n≤10
5
,n−1≤m≤10
5
,1≤u,v≤n,1≤c,d,e,w≤10
5
)

(The graph has no self-loop , no repeated edges , and is connected)

Output
Output a line for the minimum cost. Output -1−1 if she can’t reach.

样例输入 复制
5 7
1 1 1
1 2 1
1 3 5
1 4 1
2 3 2
2 4 5
3 4 3
3 5 5
样例输出 复制
5
main.cpp


一开始我在想有没有一种直接的方法找出可行的答案,但好像并没有。。
这里采用二分+最短路和二分+BFS的方法,也算是一种优雅的暴力
不管是最短路还是BFS,其目的都是找出一条从1到n满足条件的路径

二分+Dijkstra

#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const ll INF=1e10+5;
ll n,m;
ll c,d,e;
struct Node{
    ll end;
    ll val;
    Node(ll _end,ll _val):end(_end),val(_val){}
};
vector<Node> G[maxn];
ll dis[maxn];
ll edge_num[maxn]; //edge_num[i]表示最短路到i点时,经过的边数
int check(int time)
{
    fill(dis,dis+maxn,INF);
    memset(edge_num,0,sizeof(edge_num));
    priority_queue<pair<ll,int> > pq;
    dis[1]=0;
    pq.push(make_pair(0,1));
    while(!pq.empty())
    {
        int u=pq.top().second;
        ll nowdis=pq.top().first*(-1);
        pq.pop();
        if(nowdis>dis[u]) continue;
        int len=G[u].size();
        for(int i=0;i<len;++i)
        {
            int v=G[u][i].end;
            ll val=G[u][i].val;
            if(val>time*d) continue;
            if(dis[v]>dis[u]+val)
            {
                dis[v]=dis[u]+val;
                edge_num[v]=edge_num[u]+1;
                if(edge_num[v]>time*e) continue;
                pq.push(make_pair(-dis[v],v));
            }
            else if(dis[v]==dis[u]+val)
            {
                edge_num[v]=min(edge_num[v],edge_num[u]+1);
            }
        }
    }
    if(dis[n]==INF) return 0;
    return 1;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m;
    cin>>c>>d>>e;
    int u,v,w;
    for(int i=1;i<=m;++i)
    {
        cin>>u>>v>>w;
        G[u].push_back(Node(v,w));
        G[v].push_back(Node(u,w));
    }
    ll l=1,r=100000;
    int ans=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid)==1){
            ans=mid;
            r=mid-1;
        }
        else{
            l=mid+1;
        }
    }
    cout<<ans*c<<endl;
    return 0;
}

二分+BFS

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int n,m;
ll c,d,e;
struct Node{
    int end;
    ll val;
    Node(int _end,ll _val):end(_end),val(_val){}
};
vector<Node> G[maxn];
bool vis[maxn];
int step[maxn]; 
bool check(int time) //更新time次,能否到达终点
{
    memset(vis,false,sizeof(vis));
    queue<int> q;
    q.push(1);
    vis[1]=true;
    while(!q.empty())
    {
        int now=q.front();
        if(now==n) return true;
        q.pop();
        int len=G[now].size();
        for(int i=0;i<len;++i)
        {
            int v=G[now][i].end;
            ll val =G[now][i].val;
            if(vis[v]==false&&val<=time*d&&step[now]+1<=time*e){
                q.push(v);
                vis[v]=true;
                step[v]=step[now]+1;
            }
            else if(vis[v]==true&&step[v]>step[now]+1&&step[now]+1<=time*e){
                q.push(v);
                step[v]=step[now]+1;
            }
        }
    }
    return false;
}
int main()
{
    cin>>n>>m;
    cin>>c>>d>>e;
    for(int i=1;i<=m;++i)
    {
        ll u,v,w;
        cin>>u>>v>>w;
        G[u].push_back(Node(v,w));
        G[v].push_back(Node(u,w));
    }
    ll l=1,r=maxn;
    ll ans;
    while(l<=r)
    {
        ll mid=(l+r)/2;
        if(check(mid)==1){
            ans=mid;
            r=mid-1;
        }
        else {
            l=mid+1;
        }
    }
    cout<<ans*c<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值