The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5974 Accepted Submission(s): 1365
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.
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 <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), which means there is an extra edge, connecting a pair of node u and v, with cost w.
For each test case, first line has three numbers N, M (0 <= N, M <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), 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.
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
Recommend
zhuyuanchen520
题意:有n个点,m条无向边 ,每条边都是有权值, 并且每个点属于一个楼层 ,楼层上的点到可以到相邻楼层的任意一点 ,但是要花费c。没有点的相邻楼层不能互达。求 1 到 n的最小花费。
解题思路:图建好了就是最短路了。把楼层看成一个点,第i层可以看成第n+i个点。楼层与该楼层上的点建边,边权为0,单向;相邻楼层上的点与该楼层建边,边权为C,单向。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <functional>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const double exps=1e-8;
int n,m,c;
int a[100050];
bool vis[200050];
int dis[200050],s[200050],nt[2000050],e[20000050],l[20000050],cnt;
struct node
{
int id,dis;
friend bool operator <(node a,node b)
{
return a.dis>b.dis;
}
};
void addedge(int u,int v,int w)
{
nt[cnt]=s[u];
s[u]=cnt;
e[cnt]=v;
l[cnt++]=w;
}
void Dijkstra()
{
memset(vis,0,sizeof vis );
memset(dis,INF,sizeof dis);
priority_queue<node>q;
dis[1]=0;
node pre,nt1;
pre.dis=0,pre.id=1;
q.push(pre);
while(!q.empty())
{
pre=q.top();
q.pop();
if(vis[pre.id]) continue;
vis[pre.id]=1;
for(int i=s[pre.id];~i;i=nt[i])
{
int ee=e[i];
if(dis[ee]>dis[pre.id]+l[i])
{
dis[ee]=dis[pre.id]+l[i];
nt1.id=ee;
nt1.dis=dis[ee];
q.push(nt1);
}
}
}
}
int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&c);
memset(s,-1,sizeof s);
cnt=1;
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
int u,v,w;
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
for(int i=1; i<=n; i++)
{
addedge(a[i]+n,i,0);
if(a[i]>1) addedge(i,a[i]-1+n,c);
if(a[i]<n) addedge(i,a[i]+1+n,c);
}
Dijkstra();
printf("Case #%d: %d\n",cas++,dis[n]>=INF?-1:dis[n]);
}
return 0;
}