我都不好意思发题解了,看这篇博吧。(飞行路线的,基本一样)
http://blog.csdn.net/vmurder/article/details/40075989
同学做了好久,我害怕题里有坑,又重写了一遍~~~
7分钟,都不乐意测样例测点就A了啊哈。
#include <queue> #include <cstdio> #include <cstring> #include <algorithm> #define N 55 #define M 1010 #define K 50 #define inf 0x3f3f3f3f using namespace std; struct KSD { int v,len,next; }e[M<<1]; int head[N],cnt; void add(int u,int v,int len) { cnt++; e[cnt].v=v; e[cnt].len=len; e[cnt].next=head[u]; head[u]=cnt; } struct Lux { int x,y; Lux(int a,int b):x(a),y(b){} Lux(){} }; int n,m,p; int dist[K][N],s,t; bool in[K][N]; int spfa() { int i,v; queue<Lux>q; memset(dist,0x3f,sizeof(dist)); dist[0][s]=0; in[0][s]=1; q.push(Lux(0,s)); while(!q.empty()) { Lux U=q.front();q.pop(); for(i=head[U.y];i;i=e[i].next) { v=e[i].v; if(dist[U.x][v]>dist[U.x][U.y]+e[i].len) { dist[U.x][v]=dist[U.x][U.y]+e[i].len; if(!in[U.x][v]) { in[U.x][v]=1; q.push(Lux(U.x,v)); } } } if(U.x<p)for(i=head[U.y];i;i=e[i].next) { v=e[i].v; if(dist[U.x+1][v]>dist[U.x][U.y]+(e[i].len>>1)) { dist[U.x+1][v]=dist[U.x][U.y]+(e[i].len>>1); if(!in[U.x+1][v]) { in[U.x+1][v]=1; q.push(Lux(U.x+1,v)); } } } } int ret=inf; for(i=0;i<=p;i++)ret=min(ret,dist[i][t]); return ret; } int main() { int i,j,k; int a,b,c; scanf("%d%d%d",&n,&m,&p); for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,c); } s=1,t=n; printf("%d\n",spfa()); return 0; }
时间: 2024-10-26 03:11:00