一种方法两种形式(dijkstra):
Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cost to the destination. There's a problem here: Shua Shua has a special credit card which can reduce half the price of a ticket ( i.e. 100 becomes 50, 99 becomes 49. The original and reduced price are both integers. ). But he can only use it once. He has no idea which flight he should choose to use the card to make the total cost least. Can you help him?
Input
There are no more than 10 test cases. Subsequent test cases are separated by a blank line.
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000
0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters.
Output
One line for each test case the least money Shua Shua have to pay. If it's impossible for him to finish the trip, just output -1.
Sample Input
4 4 Harbin Beijing 500 Harbin Shanghai 1000 Beijing Chengdu 600 Shanghai Chengdu 400 Harbin Chengdu 4 0 Harbin Chengdu
Sample Output
800 -1
Hint
In the first sample, Shua Shua should use the card on the flight from
Beijing to Chengdu, making the route Harbin->Beijing->Chengdu have the
least total cost 800. In the second sample, there's no way for him to get to
Chengdu from Harbin, so -1 is needed.
第一种:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
#define INF 1e17//这里wa了一下午
typedef long long ll;
struct node{
ll id;
ll d;
};
vector<node> p[100001];
vector<node> p1[100001];
struct Node{
ll id;
ll d;
bool operator <(const Node &r)const
{
return d>r.d;
}
}dis1[100001],dis2[100001];
map<string,ll> M;
ll flag[100001],f[100001],m,n;
//两次djkstra分别求出st到每个点的最短路和en到每个点的最短路
void dj(ll st,ll en){
for(ll i=1;i<=n;i++){
flag[i]=0;
f[i]=i;
dis1[i].id=i;
dis1[i].d=INF;
}
dis1[st].d=0;
priority_queue<Node> q;
q.push(dis1[st]);
flag[st]=1;
while(!q.empty()){
Node t=q.top();
q.pop();
ll len=p[t.id].size();
for(ll i=0;i<len;i++){
if(dis1[p[t.id][i].id].d>dis1[t.id].d+p[t.id][i].d){
dis1[p[t.id][i].id].d=dis1[t.id].d+p[t.id][i].d;
if(!flag[p[t.id][i].id]){
q.push(dis1[p[t.id][i].id]);
flag[p[t.id][i].id]=1;
f[p[t.id][i].id]=t.id;
}
}
}
}
for(ll i=1;i<=n;i++){
flag[i]=0;
dis2[i].id=i;
dis2[i].d=INF;
}
dis2[en].d=0;
//priority_queue<Node> q;
q.push(dis2[en]);
flag[en]=1;
while(!q.empty()){
Node t=q.top();
q.pop();
ll len=p1[t.id].size();
for(ll i=0;i<len;i++){
if(dis2[p1[t.id][i].id].d>dis2[t.id].d+p1[t.id][i].d){
dis2[p1[t.id][i].id].d=dis2[t.id].d+p1[t.id][i].d;
if(!flag[p1[t.id][i].id]){
q.push(dis2[p1[t.id][i].id]);
flag[p1[t.id][i].id]=1;
}
}
}
}
return ;
}
int main(){
while(cin>>n>>m){
M.clear();
ll cnt=1;
string a,b;
for(ll i=1;i<=m;i++){
ll d;
cin>>a>>b>>d;
if(!M.count(a)){
M[a]=cnt++;
}
if(!M.count(b)){
M[b]=cnt++;
}
node t;
t.d=d;
t.id=M[b];
//两个vector存储相反的路径
p[M[a]].push_back(t);
t.id=M[a];
p1[M[b]].push_back(t);
}
n=cnt-1;
cin>>a>>b;
if(a==b){
cout<<0<<endl;
continue;
}
if(!M.count(a)||!M.count(b)){
cout<<-1<<endl;
continue;
}
ll st=M[a];
ll en=M[b];
dj(st,en);
if(dis1[en].d==INF){
cout<<-1<<endl;
continue;
}
ll max_k=INF;
for(ll i=1;i<=n;i++){//遍历所有边
ll xx=p[i].size();
for(ll j=0;j<xx;j++){
max_k=min(max_k,dis1[i].d+dis2[p[i][j].id].d+p[i][j].d/2);
}
}
for(ll i=1;i<=n;i++){
p[i].clear();
p1[i].clear();
}
cout<<max_k<<endl;
}
return 0;
}
第二种:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
#define INF 1e17
typedef long long ll;
struct node{
ll id;
ll d;
};
vector<node> p[100001];
vector<node> p1[100001];
struct Node{
ll id;
ll d;
bool operator <(const Node &r)const
{
return d>r.d;
}
}dis1[100001],dis2[100001];
map<string,ll> M;
ll flag[100001],f[100001],m,n;
void dj(ll st,ll en){
for(ll i=1;i<=n;i++){
flag[i]=0;
f[i]=i;
dis1[i].id=i;
dis1[i].d=INF;
}
dis1[st].d=0;
priority_queue<Node> q;
ll len;
q.push(dis1[st]);
//和典型的djkstra相似
for(ll i=1;i<=n;i++){
Node t;
while(!q.empty()){
if(flag[q.top().id]==1){
q.pop();continue;
}
flag[q.top().id]=1;
t=q.top();
q.pop();
break;
}
len=p[t.id].size();
for(ll j=0;j<len;j++){
if(!flag[p[t.id][j].id]&&dis1[p[t.id][j].id].d>dis1[t.id].d+p[t.id][j].d){
dis1[p[t.id][j].id].d=dis1[t.id].d+p[t.id][j].d;
q.push(dis1[p[t.id][j].id]);
f[p[t.id][j].id]=t.id;
}
}
}
while(!q.empty()){
q.pop();
}
for(ll i=1;i<=n;i++){
flag[i]=0;
dis2[i].id=i;
dis2[i].d=INF;
}
dis2[en].d=0;
len=p1[en].size();
q.push(dis2[en]);
for(ll i=1;i<=n;i++){
Node t;
while(!q.empty()){
if(flag[q.top().id]==1){
q.pop();continue;
}
flag[q.top().id]=1;
t=q.top();
q.pop();
break;
}
len=p1[t.id].size();
for(ll j=0;j<len;j++){
if(!flag[p1[t.id][j].id]&&dis2[p1[t.id][j].id].d>dis2[t.id].d+p1[t.id][j].d){
dis2[p1[t.id][j].id].d=dis2[t.id].d+p1[t.id][j].d;
q.push(dis2[p1[t.id][j].id]);
}
}
}
while(!q.empty()){
q.pop();
}
return ;
}
int main(){
while(cin>>n>>m){
M.clear();
ll cnt=1;
string a,b;
for(ll i=1;i<=m;i++){
ll d;
cin>>a>>b>>d;
if(!M.count(a)){
M[a]=cnt++;
}
if(!M.count(b)){
M[b]=cnt++;
}
node t;
t.d=d;
t.id=M[b];
p[M[a]].push_back(t);
t.id=M[a];
p1[M[b]].push_back(t);
}
n=cnt-1;
cin>>a>>b;
if(a==b){
cout<<0<<endl;
continue;
}
if(!M.count(a)||!M.count(b)){
cout<<-1<<endl;
continue;
}
ll st=M[a];
ll en=M[b];
dj(st,en);
if(dis1[en].d==INF){
cout<<-1<<endl;
continue;
}
ll max_k=INF;
for(ll i=1;i<=n;i++){
ll xx=p[i].size();
for(ll j=0;j<xx;j++){
max_k=min(max_k,dis1[i].d+dis2[p[i][j].id].d+p[i][j].d/2);
}
}
for(ll i=1;i<=n;i++){
p[i].clear();
p1[i].clear();
}
cout<<max_k<<endl;
}
return 0;
}
一开始以为先找出最短路,再找最短路中的最长边即可,实际上不是,而是遍历图中所有的边,,,以下是一开始写错的代码(路径的记录方法还是值得记住的);
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
#define INF 1e17
typedef long long ll;
struct node{
ll id;
ll d;
};
vector<node> p[100001];
vector<node> p1[100001];
struct Node{
ll id;
ll d;
bool operator <(const Node &r)const
{
return d>r.d;
}
}dis1[100001],dis2[100001];
map<string,ll> M;
ll flag[100001],f[100001],m,n;
void dj(ll st,ll en){
for(ll i=1;i<=n;i++){
flag[i]=0;
f[i]=i;
dis1[i].id=i;
dis1[i].d=INF;
}
dis1[st].d=0;
priority_queue<Node> q;
q.push(dis1[st]);
flag[st]=1;
while(!q.empty()){
Node t=q.top();
q.pop();
ll len=p[t.id].size();
for(ll i=0;i<len;i++){
if(dis1[p[t.id][i].id].d>dis1[t.id].d+p[t.id][i].d){
dis1[p[t.id][i].id].d=dis1[t.id].d+p[t.id][i].d;
if(!flag[p[t.id][i].id]){
q.push(dis1[p[t.id][i].id]);
flag[p[t.id][i].id]=1;
f[p[t.id][i].id]=t.id;
}
}
}
}
for(ll i=1;i<=n;i++){
flag[i]=0;
dis2[i].id=i;
dis2[i].d=INF;
}
dis2[en].d=0;
q.push(dis2[en]);
flag[en]=1;
while(!q.empty()){
Node t=q.top();
q.pop();
ll len=p1[t.id].size();
for(ll i=0;i<len;i++){
if(dis2[p1[t.id][i].id].d>dis2[t.id].d+p1[t.id][i].d){
dis2[p1[t.id][i].id].d=dis2[t.id].d+p1[t.id][i].d;
if(!flag[p1[t.id][i].id]){
q.push(dis2[p1[t.id][i].id]);
flag[p1[t.id][i].id]=1;
}
}
}
}
// ll len;
// q.push(dis1[st]);
// for(ll i=1;i<=n;i++){
// Node t;
// while(!q.empty()){
// if(flag[q.top().id]==1){
// q.pop();continue;
// }
// flag[q.top().id]=1;
// t=q.top();
// q.pop();
// break;
// }
// len=p[t.id].size();
// for(ll j=0;j<len;j++){
// if(!flag[p[t.id][j].id]&&dis1[p[t.id][j].id].d>dis1[t.id].d+p[t.id][j].d){
// dis1[p[t.id][j].id].d=dis1[t.id].d+p[t.id][j].d;
// q.push(dis1[p[t.id][j].id]);
// f[p[t.id][j].id]=t.id;
// }
// }
// }
// while(!q.empty()){
// q.pop();
// }
// for(ll i=1;i<=n;i++){
// flag[i]=0;
// dis2[i].id=i;
// dis2[i].d=INF;
// }
// dis2[en].d=0;
// len=p1[en].size();
// q.push(dis2[en]);
// for(ll i=1;i<=n;i++){
// Node t;
// while(!q.empty()){
// if(flag[q.top().id]==1){
// q.pop();continue;
// }
// flag[q.top().id]=1;
// t=q.top();
// q.pop();
// break;
// }
// len=p1[t.id].size();
// for(ll j=0;j<len;j++){
// if(!flag[p1[t.id][j].id]&&dis2[p1[t.id][j].id].d>dis2[t.id].d+p1[t.id][j].d){
// dis2[p1[t.id][j].id].d=dis2[t.id].d+p1[t.id][j].d;
// q.push(dis2[p1[t.id][j].id]);
// }
// }
// }
// while(!q.empty()){
// q.pop();
// }
return ;
}
int main(){
while(cin>>n>>m){
M.clear();
ll cnt=1;
string a,b;
for(ll i=1;i<=m;i++){
ll d;
cin>>a>>b>>d;
if(!M.count(a)){
M[a]=cnt++;
}
if(!M.count(b)){
M[b]=cnt++;
}
node t;
t.d=d;
t.id=M[b];
p[M[a]].push_back(t);
t.id=M[a];
p1[M[b]].push_back(t);
}
n=cnt-1;
cin>>a>>b;
if(a==b){
cout<<0<<endl;
continue;
}
if(!M.count(a)||!M.count(b)){
cout<<-1<<endl;
continue;
}
ll st=M[a];
ll en=M[b];
dj(st,en);
if(dis1[en].d==INF){
cout<<-1<<endl;
continue;
}
// ll max_k=INF;
// for(ll i=1;i<=n;i++){
// ll xx=p[i].size();
// for(ll j=0;j<xx;j++){
// max_k=min(max_k,dis1[i].d+dis2[p[i][j].id].d+p[i][j].d/2);
// }
// }
for(ll i=en;f[i]!=i;i=f[i]){
ll len_s=p1[i].size();
for(ll j=0;j<len_s;j++){
if(p1[i][j].id==f[i]){
max_k=min(max_k,dis1[f[i]].d+dis2[i].d+p1[i][j].d/2);
break;
}
}
}
for(ll i=1;i<=n;i++){
p[i].clear();
p1[i].clear();
}
if(max_k<INF)
cout<<max_k<<endl;
else cout<<-1<<endl;
}
return 0;
}