http://www.lydsy.com/JudgeOnline/problem.php?id=2763
这也算分层图最短路?
dp[i][j]到城市i,还剩k次免费次数的最短路
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define N 10001 #define M 50001 int n,k; int S,T; int tot; int front[N],nxt[M<<1],to[M<<1],val[M<<1]; int dp[N][11]; bool vis[N][11]; struct node { int pos,rest; int dis; node(int Pos=0,int Rest=0,int Dis=0):pos(Pos),rest(Rest),dis(Dis){} bool operator < (node p) const { return dis>p.dis; } }now; priority_queue<node>q; void read(int &x) { x=0; char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } } void add(int u,int v,int w) { to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=w; to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; val[tot]=w; } void dijkstra() { memset(dp,63,sizeof(dp)); dp[S][k]=0; now.pos=S; now.rest=k; q.push(now); int u,v,cnt; while(!q.empty()) { now=q.top(); q.pop(); u=now.pos; cnt=now.rest; if(vis[u][cnt]) continue; vis[u][cnt]=true; for(int i=front[u];i;i=nxt[i]) { v=to[i]; if(dp[u][cnt]+val[i]<dp[v][cnt]) { dp[v][cnt]=dp[u][cnt]+val[i]; q.push(node(v,cnt,dp[v][cnt])); } if(cnt && dp[u][cnt]<dp[v][cnt-1]) { dp[v][cnt-1]=dp[u][cnt]; q.push(node(v,cnt-1,dp[v][cnt-1])); } } } } int main() { int m; read(n); read(m); read(k); read(S); read(T); int u,v,w; while(m--) { read(u); read(v); read(w); add(u,v,w); } dijkstra(); int ans=1e9; for(int i=0;i<=k;++i) ans=min(ans,dp[T][i]); printf("%d",ans); }
2763: [JLOI2011]飞行路线
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3831 Solved: 1460
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
Sample Output
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.