首页 > 代码库 > XDOJ_1078_分层图最短路
XDOJ_1078_分层图最短路
http://acm.xidian.edu.cn/problem.php?id=1078
模版。
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> #include<queue> #define INF 0x3f3f3f3f using namespace std; struct edge { int to,cost; friend bool operator>(edge x,edge y) { return x.cost > y.cost; } }e; vector<edge> v[11005]; int n,m,k,a[1005][1005],dis[11005],vis[11005]; priority_queue< edge,vector<edge>,greater<edge> > q; int dij() { while(!q.empty()) q.pop(); memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[1] = 0; e.to = 1; e.cost = 0; q.push(e); while(!q.empty()) { int now = q.top().to,cost = q.top().cost; if(now == (k+1)*n) return dis[(k+1)*n]; q.pop(); if(vis[now]) continue; vis[now] = 1; for(int i = 0;i < v[now].size();i++) { e = v[now][i]; if(e.cost+dis[now] < dis[e.to]) { dis[e.to] = e.cost+dis[now]; e.cost = dis[e.to]; q.push(e); } } } } int main() { while(~scanf("%d%d%d",&n,&m,&k)) { for(int i = 1;i <= 11000;i++) v[i].clear(); memset(a,0x3f,sizeof(a)); for(int i = 1;i <= m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); a[u][v] = min(a[u][v],w); a[v][u] = min(a[v][u],w); } for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++) { if(i == j || a[i][j] == INF) continue; for(int l = 0;l <= k;l++) { e.to = l*n+j; e.cost = a[i][j]; v[l*n+i].push_back(e); if(l != k) { e.to = (l+1)*n+j; e.cost = 0; v[l*n+i].push_back(e); } } } } printf("%d\n",dij()); } return 0; }
XDOJ_1078_分层图最短路
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。