Bumped!(dijskra)

题目描述

Peter returned from the recently held ACM ICPC World finals only to find that his return flight was overbooked and he was bumped from the flight! Well, at least he wasn’t beat up by the
airline and he’s received a voucher for one free flight between any two destinations he wishes.
He is already planning next year’s trip. He plans to travel by car where necessary, but he may be using his free flight ticket for one leg of the trip. He asked for your help in his planning.
He can provide you a network of cities connected by roads, the amount it costs to buy gas for traveling between pairs of cities, and a list of available flights between some of those cities. Help Peter by finding the minimum amount of money he needs to spend to get from his hometown to next year’s destination!

 

输入

The input consists of a single test case. The first line lists five space-separated integers n, m, f, s, and t, denoting the number of cities n (0 < n ≤ 50 000), the number of roads m (0 ≤ m ≤ 150 000), the number of flights f (0 ≤ f ≤ 1 000), the number s (0 ≤ s < n) of the city in which Peter’s trip starts, and the number t (0 ≤ t < n) of the city Peter is trying to travel to. (Cities are numbered from 0 to n − 1.)
The first line is followed by m lines, each describing one road. A road description contains three space-separated integers i, j, and c (0 ≤ i, j < n, i 6= j and 0 < c ≤ 50 000), indicating there is a road connecting cities i and j that costs c cents to travel. Roads can be used in either direction for the same cost. All road descriptions are unique.
Each of the following f lines contains a description of an available flight, which consists of two space-separated integers u and v (0 ≤ u, v < n, u 6= v) denoting that a flight from city u to city v is available (though not from v to u unless listed elsewhere). All flight descriptions are unique.

 

输出

Output the minimum number of cents Peter needs to spend to get from his home town to the competition,using at most one flight. You may assume that there is a route on which Peter can reach his destination.

 

样例输入

复制样例数据

8 11 1 0 5
0 1 10
0 2 10
1 2 10
2 6 40
6 7 10
5 6 10
3 5 15
3 6 40
3 4 20
1 4 20
1 3 20
4 7

样例输出

45

题意:
一个n个顶点m条边的无向图,求从s到t权值最小的路径(可飞跃一次,直接到t点不算权值)
解题思路:
最短路问题,因为可条跳跃一次(不算权值),并且可以有多个跳跃点,但只允许跳跃一次,所以在结构体重加一个变量(ope)来判断当前路径有没有跳跃过。

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
const int maxn = 50000+2;
typedef long long ll;
using namespace std;

struct Cnode{
    int k;
    ll w;
    bool op;
};
bool operator<(const Cnode &d1,const Cnode &d2){
    return d1.w>d2.w;
}
priority_queue<Cnode> pq;
bool visit[maxn];
vector<vector<Cnode> >v;
int a,b,c;
Cnode p;

int main()
{
    int n,m,f,s,t;
    cin>>n>>m>>f>>s>>t;
    v.clear();
    v.resize(n+1);
    memset(visit,0,sizeof(visit));
    for(int i = 1;i<=m;i++){
        cin>>a>>b>>c;
        p.k = b;
        p.w  = c;
        p.op = 0;
        v[a].push_back(p);
        p.k = a;
        p.w = c;
        p.op = 0;
        v[b].push_back(p);
    }
    for(int i = 1;i<=f;i++){
        cin>>a>>b;
        p.k = b;
        p.w = 0;
        p.op = 0;
        v[a].push_back(p);
    }
    p.k = s;
    p.w = 0;
    p.op = 0;
    pq.push(p);
    while(!pq.empty()){
        p = pq.top();
        pq.pop();
        if(visit[p.k])
            continue;
        visit[p.k] = true;
        if(p.k == t)
            break;
        for(int i = 0; i< v[p.k].size();i++){
            Cnode q;
            if(p.op == 1&&v[p.k][i].w == 0) //当前为跳跃点并且该路径已经跳跃过
                continue;
            q.k = v[p.k][i].k;
            if(visit[q.k])
                continue;
            if(v[p.k][i].w == 0||p.op == 1)//在此次操作前已经跳跃过,或者此次进行跳跃操作则更新为1
                q.op = 1;
            else
                q.op = 0;
            q.w = p.w+v[p.k][i].w; 
            pq.push(q);
        }
    }
    printf("%lld",p.w);
    return 0;
}
 
©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值