UPC 备战省赛第六场 Bumped!

题目描述

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个点,这n个点的编号是0到n-1,m条边起始点是s,终点是t,然后是f条距离为0的边,这f条边最多选一条,求最短的路径

分析:跑两边dijkstra的板子,先从起点s到终点t求得一个dis1,然后再从t到s求得一个dis2,这样就再去枚举这f条边就能到答案了

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int MOD=1e9+7;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Abs(x) ((x)>=0?(x):-(x))
int n,m;
int head[N*4];
ll dis1[N],dis2[N];
struct node{
    int next,to;
    ll w;
}num[N*4];
struct node1{
    int id;ll w;
    bool operator < (const node1 & a) const{
        return w>a.w;
    }
    node1(int id_,ll w_):id(id_) ,w(w_){}
};
int tot;
bool vis[N*4];
void add(int u,int v,ll w){
    tot++;
    num[tot].w=w;
    num[tot].to=v;
    num[tot].next=head[u];
    head[u]=tot;
}
void d(int s,ll (&dis)[N]){
    memset(dis,INF,sizeof dis);
    memset(vis,false,sizeof vis);
    dis[s]=0;
    priority_queue<node1>p;
    p.push(node1(s,dis[s]));
    while(!p.empty()){
        node1 u=p.top();
        p.pop();
        if(vis[u.id]) continue;
        vis[u.id]=1;
        for(int i=head[u.id];i!=0;i=num[i].next){
            int to=num[i].to;
            ll w=num[i].w;
            if(!vis[to]&&dis[to]>dis[u.id]+w){
                dis[to]=dis[u.id]+w;
                p.push(node1(to,dis[to]));
            }
        }
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int f,s,t;
    scanf("%d%d%d%d%d",&n,&m,&f,&s,&t);
    for(int i=0;i<m;i++){int u,v;ll w;
        scanf("%d%d%lld",&u,&v,&w);
        add(u,v,w);add(v,u,w);
    }
    d(s,dis1);
    d(t,dis2);
    ll ans=dis1[t];
    for(int i=1;i<=f;i++){int s1,s2;
        scanf("%d%d",&s1,&s2);
        ans=min(ans,dis1[s1]+dis2[s2]);
    }
    printf("%lld\n",ans);
    return 0;
 }

emm,这题还可以用SPFA去解决

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int MOD=1e9+7;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Abs(x) ((x)>=0?(x):-(x))
int n,m;
int head[N*4];
ll dis1[N];
struct node{
    int next,to;
    ll w;
}num[N*4];
int tot;
bool vis[N*4];
void add(int u,int v,ll w){
    tot++;
    num[tot].w=w;
    num[tot].to=v;
    num[tot].next=head[u];
    head[u]=tot;
}
void SPFA(int s){
    queue<int>q;
    q.push(s);
    vis[s]=1;
    memset(dis1,INF,sizeof dis1);
    dis1[s]=0;
    while(q.size()){
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=0;i=num[i].next){
                int to=num[i].to;
                ll w=num[i].w;
            if(dis1[to]>dis1[u]+w){
               dis1[to] =dis1[u]+w;
               if(!vis[to]){
                q.push(to);
                vis[to]=1;
               }
            }
        }
        vis[u]=0;
    }
}
void SSPFA(int s){
    queue<int>q;
    q.push(s);

    vis[s]=1;
    while(q.size()){
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=0;i=num[i].next){
                int to=num[i].to;
                ll w=num[i].w;
            if(dis1[to]>dis1[u]+w){
               dis1[to] =dis1[u]+w;
               if(!vis[to]){
                q.push(to);
                vis[to]=1;
               }
            }
        }
        vis[u]=0;
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int f,s,t;
    scanf("%d%d%d%d%d",&n,&m,&f,&s,&t);
    for(int i=0;i<m;i++){int u,v;ll w;
        scanf("%d%d%lld",&u,&v,&w);
        add(u,v,w);add(v,u,w);
    }
    SPFA(s);
    ll ans=dis1[t];
    for(int i=1;i<=f;i++){int s1,s2;
        scanf("%d%d",&s1,&s2);
        add(s1,s2,0);
        SSPFA(s1);
        num[tot].w=INF;
        ans=min(ans,dis1[t]);
    }
    printf("%lld\n",ans);
    return 0;
 }

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/89321491
UPC