Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?
标签:
多加一维进去dijkstra就可以了。。好像usaco有过。。
#include<cstdio> #include<cstring> #include<cctype> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define dwn(i,s,t) for(int i=s;i>=t;i--) #define clr(x,c) memset(x,c,sizeof(x)) #define qwq(x) for(edge *o=head[x];o;o=o->next) int read(){ int x=0;char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) x=x*10+c-‘0‘,c=getchar(); return x; } const int nmax=1e4+5; const int maxn=1e5+5; const int inf=0x7f7f7f7f; struct edge{ int to,dist;edge *next; }; edge es[maxn],*pt=es,*head[nmax]; void add(int u,int v,int d){ pt->to=v;pt->dist=d;pt->next=head[u];head[u]=pt++; pt->to=u;pt->dist=d;pt->next=head[v];head[v]=pt++; } int dist[nmax][11]; struct node{ int x,k,dist; node(int x,int k,int dist):x(x),k(k),dist(dist){}; node(){}; bool operator<(const node&rhs)const{ return dist>rhs.dist;} }; priority_queue<node>q; void dijkstra(int s,int t,int k){ clr(dist,0x7f);dist[s][0]=0;q.push(node(s,0,0)); node o;int ta,td,tk,to; while(!q.empty()){ o=q.top();q.pop();ta=o.x,td=o.dist,tk=o.k; if(dist[ta][tk]!=td) continue; qwq(ta){ to=o->to; if(dist[o->to][tk]>td+o->dist){ dist[o->to][tk]=td+o->dist;q.push(node(o->to,tk,td+o->dist)); } if(tk<k&&dist[o->to][tk+1]>td){ dist[o->to][tk+1]=td;q.push(node(o->to,tk+1,td)); } } } int ans=inf; rep(i,0,k) ans=min(ans,dist[t][i]); printf("%d\n",ans); return ; } int main(){ int n=read(),m=read(),k=read(),u,v,d,s=read(),t=read(); rep(i,1,m) u=read(),v=read(),d=read(),add(u,v,d); dijkstra(s,t,k); return 0; }
对于30%的数据,2<=n<=50,1<=m<=300,k=0;
对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;
对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10.
标签:
原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5885032.html