链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2763
题意:中文题。。
分析:建分层图,由于是双向边,在相邻两层图中也加上反向边。然后跑一边heap+dijkstra。
代码:
#include <algorithm>
#include <iostream>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define INF 0x3f3f3f3f
#define Mn 300010
#define Mm 3000010
#define mod 1000000007
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define CPY(a,b) memcpy ((a), (b), sizeof((a)))
#pragma comment(linker, "/STACK:102400000,102400000")
#define ul (u<<1)
#define ur (u<<1)|1
using namespace std;
typedef long long ll;
struct edge {
int v,next,w;
}e[Mm];
struct node {
int v,cost;
node(){}
node(int x,int y):v(x),cost(y){}
bool operator <(const node a) const {
return a.cost<cost;
}
};
int tot,head[Mn];
void addedge(int u,int v,int w) {
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
priority_queue<node> q;
int n,m,k;
int dis[Mn],vis[Mn];
int ans;
void dijkstra(int st,int ed) {
while(!q.empty()) q.pop();
q.push(node(st,0));
for(int i=0;i<=(k+1)*n;i++) {
dis[i]=INF;
vis[i]=0;
}
dis[st]=0;
while(!q.empty()) {
int u=q.top().v;
q.pop();
if(vis[u]) continue;
vis[u]=1;
for(int i=head[u];~i;i=e[i].next) {
int v=e[i].v;
int cost=e[i].w;
if(!vis[v]&&dis[v]>dis[u]+cost) {
dis[v]=dis[u]+cost;
q.push(node(v,dis[v]));
}
}
}
for(int i=0;i<=k;i++)
ans=min(ans,dis[i*n+ed]);
}
void init() {
tot=0;
CLR(head,-1);
}
int main() {
init();
int st,ed;
scanf("%d%d%d%d%d",&n,&m,&k,&st,&ed);
int u,v,w;
while(m--) {
scanf("%d%d%d",&u,&v,&w);
for(int j=0;j<=k;j++) {
addedge(j*n+u,j*n+v,w);
addedge(j*n+v,j*n+u,w);
if(j<k) {
addedge(j*n+u,(j+1)*n+v,0);
addedge(j*n+v,(j+1)*n+u,0);
}
}
}
ans=INF;
dijkstra(st,ed);
printf("%d\n",ans);
return 0;
}
另外一种写法更好理解
#include <algorithm>
#include <iostream>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define INF 0x3f3f3f3f
#define Mn 30010
#define Mm 300010
#define mod 1000000007
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define CPY(a,b) memcpy ((a), (b), sizeof((a)))
#pragma comment(linker, "/STACK:102400000,102400000")
#define ul (u<<1)
#define ur (u<<1)|1
using namespace std;
typedef long long ll;
struct edge {
int v,next,w;
}e[Mm];
struct node {
int v,x,cost;
node(){}
node(int v,int cost,int x):v(v),cost(cost),x(x){}
bool operator <(const node a) const {
return a.cost<cost;
}
};
int tot,head[Mn];
void addedge(int u,int v,int w) {
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
priority_queue<node> q;
int n,m,k;
int dis[Mn][11],vis[Mn][11];
int ans;
void dijkstra(int st,int ed) {
while(!q.empty()) q.pop();
q.push(node(st,0,0));
for(int i=0;i<n;i++) {
for(int j=0;j<=k;j++) {
dis[i][j]=INF;
vis[i][j]=0;
}
}
dis[st][0]=0;
while(!q.empty()) {
int u=q.top().v;
int x=q.top().x;
q.pop();
if(vis[u][x]) continue;
vis[u][x]=1;
for(int i=head[u];~i;i=e[i].next) {
int v=e[i].v;
int cost=e[i].w;
if(!vis[v][x]&&dis[v][x]>dis[u][x]+cost) {
dis[v][x]=dis[u][x]+cost;
q.push(node(v,dis[v][x],x));
}
if(x<k) {
if(!vis[v][x+1]&&dis[v][x+1]>dis[u][x]) {
dis[v][x+1]=dis[u][x];
q.push(node(v,dis[v][x+1],x+1));
}
}
}
}
for(int i=0;i<=k;i++)
ans=min(ans,dis[ed][i]);
}
void init() {
tot=0;
CLR(head,-1);
}
int main() {
init();
int st,ed;
scanf("%d%d%d%d%d",&n,&m,&k,&st,&ed);
int u,v,w;
while(m--) {
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
ans=INF;
dijkstra(st,ed);
printf("%d\n",ans);
return 0;
}