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!
Input
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≤50000), the number of roads m (0≤m≤150000), the number of flights f (0≤f≤1000), 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≠j and 0<c≤50000), 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≠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
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.
Sample Input 1 | Sample Output 1 |
---|---|
| |
Sample Input 2 | Sample Output 2 |
---|---|
| |
https://cn.vjudge.net/contest/306910#problem/B
题意:给定n个点(0~n-1)之间m条无向边,然后给你f条有向边,权值为0,你可以使用这f条边的至多一条边,求s点到t点的最短路
枚举不使用0边,分别使用f条边中的每一条的情况,求最小值;
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
struct node
{
long long next,to,w;
} edge[300060];
long long head[50050],cnt,dis[50050],flag[50050];
void add(long long u, long long v, long long w)
{
edge[cnt].w = w;
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt ++;
}
void add2(long long u, long long v, long long w)
{
edge[cnt].w = w;
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt;
}
void spfa(long long s, long long t)
{
queue <long long > q;
long long x,y,i;
memset(dis,0x3f3f3f3f,sizeof(dis));
memset(flag,0,sizeof(flag));
q.push(s);
dis[s] = 0;
flag[s] = 1;
while(!q.empty())
{
x = q.front();
q.pop();
flag[x] = 0;
for(i = head[x]; i != -1; i = edge[i].next)
{
y = edge[i].to;
if(dis[y] > dis[x] + edge[i].w)
{
dis[y] = dis[x] +edge[i].w;
if(!flag[y])
{
q.push(y);
flag[y] = 1;
}
}
}
}
}
int main()
{
long long i,m,n,s,t,f,aa,bb,cc,ans,minn,x;
scanf("%lld %lld %lld %lld %lld", &n, &m, &f, &s, &t);
cnt = 0;
memset(head,-1,sizeof(head));
for(i = 0; i < m; i ++)
{
scanf("%lld %lld %lld",&aa, &bb, &cc);
add(aa,bb,cc);
add(bb,aa,cc);
}
spfa(s,t);//不使用0边的最短路
minn = dis[t];
while(f --)//分别使用f中一条边的最短路
{
scanf("%lld %lld",&aa, &bb);
x = head[aa];//首先记录以aa为起点的原来的第一条边的位置
add2(aa,bb,0);//此函数中没有cnt ++,只存一条边
spfa(s,t);
ans = dis[t];
minn = min(minn,dis[t]);
head[aa] = x;//相当于删除这条边
}
printf("%lld\n",minn);
}