1. 程式人生 > >Airport Express UVA - 11374

Airport Express UVA - 11374

復雜度 輸出 lib expr exp 最短 -- fin air

題意:

  n個點,m條固定邊,k條可選邊,可選邊最多只能選擇一條,也可以不選擇,問起點s到終點t的最小權值是什麽?

題解:

1.正確的解法:

  以起點終點為源點,求固定邊最短路,然後對於k條可選邊,比如可選邊為起點p,終點q,則含有本條可選邊的最優路徑的距離為ds[p]+dt[q]+w(w為可選邊權值,ds[p]為p到起點最短距離,dt[q]為q到終點最短距離)

  復雜度:mlogn+k

2.歪解:

  自己寫看到這道題n只有500,m只有1000,直接求K次最短路,暴力求解,之後過了。。。。。。

坑點:

  每組數據輸出時中間隔一行,最後一組數據之後不要空行

代碼:

#include<iostream>
using
namespace std; #include<cstdio> #include<cstdlib> #include<vector> #define MAXN 1000 #include<vector> #include<queue> typedef long long LL; struct teamdata{ LL d;LL point; bool operator<(const teamdata& pt)const{ return this->d>pt.d; } };
struct bian{ LL d,point; }; LL n,m,k,l,s,t; LL pre[MAXN]; vector<LL> ansl; vector<bian> maps[MAXN]; const LL inf=0x3f3f3f3f; LL d[MAXN]; queue<teamdata> team; LL SPFA(LL s,LL t){ LL ans=-1; teamdata pt;pt.d=0;pt.point=s; while(!team.empty()) team.pop(); team.push(pt);teamdata pd;
for (LL i=0;i<=MAXN-10;i++) d[i]=inf,pre[i]=-1;d[s]=0; while(!team.empty()){ pt=team.front();team.pop(); if (pt.d>d[pt.point]) continue; if (pt.point==t){ ans=pt.d;continue; } for (LL i=0;i<maps[pt.point].size();i++){ LL nb=maps[pt.point][i].point; if (d[nb]>pt.d+maps[pt.point][i].d){ d[nb]=pt.d+maps[pt.point][i].d; pre[nb]=pt.point; pd.d=d[nb];pd.point=nb;team.push(pd); } } } return ans; } int main(){ int sp=0; while(scanf("%lld%lld%lld",&n,&s,&t)!=EOF){ sp++; if (sp>1) putchar(\n); scanf("%lld",&m); bian pt; for (int i=0;i<=n;i++) maps[i].clear(); for (LL i=0;i<m;i++){ LL p,q,k; scanf("%lld%lld%lld",&p,&q,&k); pt.d=k;pt.point=q; maps[p].push_back(pt); pt.point=p; maps[q].push_back(pt); } scanf("%lld",&k); LL zans=SPFA(s,t); ansl.clear(); LL x=t; while(x>0) { ansl.push_back(x); x=pre[x]; } LL bj=-1,bjm=-1; for (LL i=0;i<k;i++){ LL p,q,k; scanf("%lld%lld%lld",&p,&q,&k); bian pt;pt.d=k;pt.point=q; maps[p].push_back(pt); pt.point=p; maps[q].push_back(pt); LL zz=SPFA(s,t); if (zz<zans&&zz>=0) { zans=zz; if (pre[q]==p) bj=p,bjm=q; else bj=q,bjm=p; ansl.clear(); LL x=t; while(x>0) { ansl.push_back(x); x=pre[x]; } } maps[p].pop_back(); maps[q].pop_back(); } int ppt=-1,used=0; for (LL i=ansl.size()-1;i>=0;i--){ printf("%lld",ansl[i]); if (i>0) putchar( ); if (bjm==ansl[i]&&bj==ppt) used=1; ppt=ansl[i]; } putchar(\n); if (used){ printf("%lld\n",bj); } else{ printf("Ticket Not Used\n"); } printf("%lld\n",zans); } return 0; }

Airport Express UVA - 11374