hdu 4725 The Shortest Path in Nya Graph(最短路)
2015-07-27 23:48
169 查看
The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3657 Accepted Submission(s): 852
Problem Description
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.
The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total.
You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost.
Besides, there are M extra edges, each connecting a pair of node u and v, with cost w.
Help us calculate the shortest path from node 1 to node N.
Input
The first line has a number T (T <= 20) , indicating the number of test cases.
For each test case, first line has three numbers N, M (0 <= N, M <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), which means there is an extra edge, connecting a pair of node u and v, with cost w.
Output
For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
If there are no solutions, output -1.
Sample Input
2 3 3 3 1 3 2 1 2 1 2 3 1 1 3 3 3 3 3 1 3 2 1 2 2 2 3 2 1 3 4
Sample Output
Case #1: 2 Case #2: 3
Source
2013 ACM/ICPC Asia Regional Online —— Warmup2
Recommend
zhuyuanchen520
题意:有n个点 , m条无向边 ,每条边都是有权值, 并且每个点属于一个楼层 ,楼层上的点到可以到相邻楼层的任意一点 ,但是要花费 c 。
没有点的相邻楼层不能互达。求 1 到 n的最小花费。
题解:图建好了就是裸的最短路了。但是建图有点麻烦,参考了别人的代码之后才明白为什么要这样建图。
把楼层看成一个点,第i层可以看成第n+i个点。楼层与该楼层上的点建边,边权为0,单向;楼层与
相邻楼层建边,边权为C,双向;相邻楼层上的点与该楼层建边,边权为C,单向。
#include<cstring> #include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cmath> #include<vector> #include<queue> #define N 200100 #define ll long long using namespace std; const int inf=0x3f3f3f3f; int n,m,c; int lay ; bool hava ; bool vis ; int cnt ; int dist ; struct Edge { int v; int cost; Edge(int _v=0,int _c=0):v(_v),cost(_c) {} bool operator <(const Edge &r)const { return cost>r.cost; } }; vector<Edge>E ; void addedge(int u,int v,int w) { Edge it; it.v=v; it.cost=w; E[u].push_back(it); } void Dijk(int n,int start) { //点的编号从1开始 memset(vis,false,sizeof(vis)); for(int i=1; i<=n*2; i++)dist[i]=inf; priority_queue<Edge>que; while(!que.empty())que.pop(); dist[start]=0; que.push(Edge(start,0)); Edge tmp; while(!que.empty()) { tmp=que.top(); que.pop(); int u=tmp.v; if(vis[u])continue; vis[u]=true; for(int i=0; i<E[u].size(); i++) { int v=E[tmp.v][i].v; int cost=E[u][i].cost; if(!vis[v]&&dist[v]>dist[u]+cost) { dist[v]=dist[u]+cost; que.push(Edge(v,dist[v])); } } } } int main() { // freopen("test.in","r",stdin); int t; cin>>t; int ca=1; while(t--) { for(int i=0; i<=n*2+1; i++)E[i].clear(); scanf("%d%d%d",&n,&m,&c); memset(hava,0,sizeof hava); for(int i=1; i<=n; i++) { scanf("%d",&lay[i]); hava[lay[i]]=true; } int u,v,cost; for(int i=1; i<=m; i++) { scanf("%d%d%d",&u,&v,&cost); addedge(u,v,cost); addedge(v,u,cost); } if(n<=1) { printf("Case #%d: 0\n",ca++); continue; } for(int i=1; i<n; i++) { if(hava[i]&&hava[i+1]) { addedge(n+i,n+i+1,c); addedge(n+1+i,n+i,c); } } for(int i=1; i<=n; i++) { addedge(lay[i]+n,i,0); if(lay[i]>1)addedge(i,lay[i]-1+n,c); if(lay[i]<n)addedge(i,lay[i]+1+n,c); } Dijk(n,1); printf("Case #%d: %d\n",ca++,dist >=inf?-1:dist ); } return 0; }
相关文章推荐
- ENV and IRB
- apk防止重新签名
- 20150727
- 导入项目 R.java没有
- 轻装上阵,安卓工程师之路---day06(HTTP & Servlet)
- ncurses简单的一个多窗口程序
- 原创jQuery插件之loadmore
- bzoj1179
- poj2109&nyoj773
- [黑马程序员](第40天)一些平时刷的题
- 使用Fragment(3)
- Wormholes (POJ 3259)
- Android图像格式类及图像转换方法
- [黑马程序员](第17)异常
- 通过统计工具做数据分析、优化和挖掘
- Basic Calculator 基本计算器-Leetcode
- 表格布局和线性布局
- 移动H5前端性能优化指南
- java学习笔记(五)collections--vector
- Android第一夜