The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12241 Accepted Submission(s): 2641
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
问题链接:HDU4725 The Shortest Path in Nya Graph
问题描述:给定n个点(从1开始编号),m条双向边,每个点属于一个层次(1~n),一个层次可能包含多个点。第i层和第i+1层的结点可以相互访问,距离为c,同一层的结点之间如果没有边相连就不能达,为从结点1到结点n的最短路径
解题思路:问题的关键是如何处理相邻层中的结点的相互访问,如果结点个数不多,直接就让第i层中的每个结点和第i+1层中的每个结点建立一条双向边,但是最多有10^5个结点,这样会超内存,解决的办法就是添加一些点,使得能实现相邻层之间的相互访问,如何添加点,见程序注释,可以自己画图,这样会更方便理解。
AC的C++程序:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int N=100100;
const int INF=0x3f3f3f3f;
int dist[3*N];//使用3*N是考虑了可能添加的结点
bool vis[3*N];//使用3*N是考虑了可能添加的结点
struct Edge{
int v,w;
Edge(int v,int w):v(v),w(w){}
};
struct Node{
int u,w;
Node(){}
Node(int u,int w):u(u),w(w){}
bool operator<(const Node &a)const
{
return w>a.w;
}
};
vector<Edge>g[3*N];//使用3*N是考虑了可能添加的结点
vector<int>layer[N];//layer[i]存储在第i层的结点
void dijkstra(int s)
{
memset(dist,INF,sizeof(dist));
memset(vis,false,sizeof(vis));
priority_queue<Node>q;
dist[s]=0;
q.push(Node(s,0));
while(!q.empty()){
Node f=q.top();
q.pop();
int u=f.u;
if(!vis[u]){
vis[u]=true;
for(int i=0;i<g[u].size();i++){
int v=g[u][i].v;
if(!vis[v]&&dist[v]>dist[u]+g[u][i].w){
dist[v]=dist[u]+g[u][i].w;
q.push(Node(v,dist[v]));
}
}
}
}
}
int main()
{
int T,n,m,c,u,v,w,l;
scanf("%d",&T);
for(int t=1;t<=T;t++){
scanf("%d%d%d",&n,&m,&c);
for(int i=0;i<=n;i++)
layer[i].clear();
for(int i=0;i<=3*n;i++)//2n表示上次操作可能添加了2*n个结点
g[i].clear();
for(int i=1;i<=n;i++){
scanf("%d",&l);
layer[l].push_back(i);//第l层有结点i
}
while(m--){
scanf("%d%d%d",&u,&v,&w);
g[u].push_back(Edge(v,w));
g[v].push_back(Edge(u,w));
}
//层与层之间建边
int node=n+1;//需要添加的点的编号
//按层数递增的顺序实现i能访问i+1层:使用一个结点进行沟通
for(int i=1;i<n;i++)
if(!layer[i].empty()&&!layer[i+1].empty()){//如果第i层和第i+1层有结点
//第i层的各个结点到添加结点node的距离为c,结点node到第i+1层的结点的距离
//为0这样就实现了第i层结点访问第i+1层结点的距离为c
for(int j=0;j<layer[i].size();j++){
int u=layer[i][j];
g[u].push_back(Edge(node,c));
}
for(int j=0;j<layer[i+1].size();j++){
int v=layer[i+1][j];
g[node].push_back(Edge(v,0));
}
node++;
}
//按层数递减的顺序实现i访问i-1层:使用一个结点进行沟通
for(int i=n;i>1;i--)
if(!layer[i].empty()&&!layer[i-1].empty()){//如果第i层和第i-1层有结点
//第i层的各个结点到添加结点node的距离为c,结点node到第i-1层的结点的距离
//为0这样就实现了第i层结点到第i-1层结点的距离为c
for(int j=0;j<layer[i].size();j++){
int u=layer[i][j];
g[u].push_back(Edge(node,c));
}
for(int j=0;j<layer[i-1].size();j++){
int v=layer[i-1][j];
g[node].push_back(Edge(v,0));
}
node++;
}
dijkstra(1);
printf("Case #%d: ",t);
if(dist[n]==INF)
printf("-1\n");
else
printf("%d\n",dist[n]);
}
return 0;
}