问题 B: Bumped!

问题 B: Bumped!
时间限制: 1 Sec 内存限制: 128 MB
提交: 374 解决: 49
[提交] [状态] [命题人:admin]
题目描述
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

题意:

给出图,求起点到终点的最少花费,但是会提供给一些免费的机票(机票是单程的),你可以之多选择一张机票(当然可以不选),求所有选择中花费最少是多少。

思路:

迪杰斯特拉优化算法,先跑一边不用机票的情况(因为机票可能为0),然后朱哥枚举所有机票的最短路,取最小即可

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
using namespace std;
#include <vector>

const ll inf = 1e10;
const ll maxn = 150010;

struct qnode{
    int v;
    ll c;
    qnode(int _v = 0,ll _c = 0):v(_v),c(_c){}
    bool operator < (const qnode &r)const
    {
        return c>r.c;
    }
};

struct Edge{
    int v;
    ll cost;
    Edge(int _v=0,ll _cost=0):v(_v),cost(_cost){}
};
vector<Edge>E[maxn];
bool vis[maxn];
ll dist[maxn];

void Dijkstra(int n,int st){
    memset(vis,0,sizeof(vis));
    for(int i=0;i<n;i++){
        dist[i]=inf;
    }
    priority_queue<qnode>que;
    while(!que.empty())que.pop();
    dist[st]=0;
    que.push(qnode(st,0));
    qnode tmp;
    while(!que.empty()){
        tmp=que.top();
        que.pop();
        int u=tmp.v;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=0;i<E[u].size();i++){
            int v=E[u][i].v;
            ll cost = E[u][i].cost;
            if(!vis[v]&&dist[v]>dist[u]+cost){
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}

void addedge(int u,int v,ll w){
    E[u].push_back(Edge(v,w));
}

int main(){
    int n,m,f,s,t;
    scanf("%d%d%d%d%d",&n,&m,&f,&s,&t);
    for(int i=0;i<m;i++){
        int st,ed,c;
        cin>>st>>ed>>c;
        addedge(st,ed,c);
        addedge(ed,st,c);
    }
    ll min1=inf;
    Dijkstra(n,s);
    min1=min(min1,dist[t]);
    for(int i=0;i<f;i++){
        int st,ed;
        cin>>st>>ed;
        addedge(st,ed,0);
        Dijkstra(n,s);
        min1=min(min1,dist[t]);
        E[st].erase(E[st].end()-1);
    }
    cout<<min1<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值