题干:

Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?

Input

数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。

第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t<n)

接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b<n,a与b不相等,0<=c<=1000)

 

Output

 

只有一行,包含一个整数,为最少花费。

Sample Input

5 6 1 0 4 0 1 5 1 2 5 2 3 5 3 4 5 2 3 3 0 2 100

Sample Output

8

Hint

 

对于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.

 

解题报告:

  注意一下数据范围,k=10,所以你需要乘以11,,,这题控制不好数据范围很容易就RE了、、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 10000 + 5;
const int MAXM = 50000 + 5;
struct edge {
	int to, ne;
	int w;
} e[MAXM*4*11];
struct node {
	int u, c;
	node() {}
	node(int u, int c) : u(u), c(c) {}
	bool operator < (const node &a) const {
		return c > a.c;
	}
};
int head[MAXN*11], tot;
int dis[MAXN*11];
bool vis[MAXN*11];
int n, m, k;
int st,ed;
void add(int u, int v, int w) {
	e[++tot].to = v;
	e[tot].ne = head[u];
	e[tot].w = w;
	head[u] = tot;
}

void Dijkstra(int st) {
	memset(dis, INF, sizeof dis);
	dis[st] = 0;
	priority_queue<node> pq;
	pq.push(node(st, 0));
	while(!pq.empty()) {
		node cur = pq.top(); pq.pop();
		if(vis[cur.u]) continue;
		vis[cur.u] = 1;
		for(int i=head[cur.u]; i!=-1; i=e[i].ne) {
			int v = e[i].to;
			if(dis[v] > cur.c+e[i].w) {
				dis[v] = cur.c+e[i].w;
				pq.push(node(v,dis[v]));
			}
		}
	}
}
int main() 
{
	memset(head, -1, sizeof head);
	tot = 0;
	scanf("%d%d%d",&n,&m,&k);
	cin>>st>>ed;
	for(int i=1; i<=m; i++) {
		int u, v, w;
		scanf("%d%d%d",&u,&v,&w);
		for(int j = 0; j<=k; j++) {
			add(u+j*n,v+j*n,w);
			add(v+j*n,u+j*n,w);
		}
		for(int j = 0; j<k; j++) {
			add(u+j*n,v+(j+1)*n,0);
			add(v+j*n,u+(j+1)*n,0);
		}
	}
	Dijkstra(st);
	int ans = INF;
	for(int j = 0; j<=k; j++) {//不用~用k次,一共k+1个图 
		ans = min(ans,dis[j*n+ed]); 
	}
	printf("%d\n",ans);
	return 0 ;
}

建立分层图的代码:

for(int i=1; i<=m; ++i) {
	scanf("%d%d%d",&a,&b,&c);
	for(int j=0; j<=k; ++j) {
		add(a+j*n,b+j*n,c);
		add(b+j*n,a+j*n,c);
		if(j<k) {
			add(a+j*n,b+(j+1)*n,0);
			add(b+j*n,a+(j+1)*n,0);
		}
	}
}

我们有k次免费坐飞机的机会,那么就有k+1个图 
我们只要保证我们每一次创建的图满足i*n+a(当前位置的标号)。

 

20190508更新:最短路dp的做法。

还WA了一发,,因为写着写着就忘了是双向边了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int dis[10005][12];
bool vis[10005][12];
int head[MAX],tot;
int n,m,k,st,ed;
struct Edge {
	int to,ne,w;
} e[MAX];
void add(int u,int v,int w) {
	e[++tot].to = v;
	e[tot].w=w;
	e[tot].ne=head[u];
	head[u] = tot;
}
struct Point {
	int pos,ci,c;
	Point(int pos=0,int ci=0,int c=0):pos(pos),ci(ci),c(c){}
	bool operator<(const Point b)const {
		return c > b.c;
	} 
};
void Dijkstra() {
	priority_queue<Point> pq;
	for(int i = 1; i<=n; i++) {
		for(int j = 0; j<=k; j++) dis[i][j]=INF,vis[i][j]=0;
	}
	dis[st][0]=0;
	pq.push(Point(st,0,0));
	while(pq.size()) {
		Point cur = pq.top();pq.pop();
		if(cur.pos == ed) break;
		if(vis[cur.pos][cur.ci]) continue;
		vis[cur.pos][cur.ci] = 1;
		for(int i = head[cur.pos]; ~i; i = e[i].ne) {
			int v = e[i].to;
			if(vis[v][cur.ci] == 0) {
				if(dis[v][cur.ci] > dis[cur.pos][cur.ci] + e[i].w) {
					dis[v][cur.ci] = dis[cur.pos][cur.ci] + e[i].w;
					pq.push(Point(v,cur.ci,dis[v][cur.ci]));
				}
			}
			int tmpci = cur.ci+1;
			if(tmpci > k) continue;
			if(vis[v][tmpci] == 0) {
				if(dis[v][tmpci] > dis[cur.pos][cur.ci]) {
					dis[v][tmpci] = dis[cur.pos][cur.ci];
					pq.push(Point(v,tmpci,dis[v][tmpci]));
				}
				
			}
		}				
	}
}
int main()
{
	cin>>n>>m>>k;
	cin>>st>>ed;
	st++,ed++;
	for(int i = 1; i<=n+1; i++) head[i] = -1;
	for(int a,b,c,i = 1; i<=m; i++) {
		scanf("%d%d%d",&a,&b,&c);
		a++,b++;
		add(a,b,c);add(b,a,c);
	}
	Dijkstra();
	int ans = INF;
	for(int i = 0; i<=k; i++) {
		ans = min(ans,dis[ed][i]);
	}
	printf("%d\n",ans);
	return 0 ;
}
//21:20 - 21:39