The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3874 Accepted Submission(s): 911
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
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_back
int T;
int n,m,c;
int H[100010*3];
struct Edge
{
int v,c,next;
}edge[100010*6+100010];
int top;
int d[100010*3];
bool ok[100010*3];
void addedge(int u,int v,int c)
{
edge[top] = {v,c,H[u]};
H[u] = top++;
}
struct Qnode
{
int idx,dis;
Qnode(){};
Qnode(int _i,int _d):idx(_i),dis(_d){};
bool operator<(const Qnode&x) const
{
return dis > x.dis;
}
};
// +n 是入口 +2n是出口
int dij(int st)
{
for(int i=1;i<=3*n;i++)
{
ok[i] = false;
d[i] = 0x3f3f3f3f;
}
d[st] = 0;
priority_queue<Qnode> pq;
pq.push(Qnode(st,0));
while(!pq.empty())
{
Qnode cur = pq.top();
pq.pop();
if(ok[cur.idx]) continue;
ok[cur.idx] = true;
for(int ei=H[cur.idx];ei!=-1;ei=edge[ei].next)
{
Edge e = edge[ei];
if(!ok[e.v] && d[e.v] > d[cur.idx] + e.c)
{
d[e.v] = d[cur.idx] + e.c;
pq.push(Qnode(e.v,d[e.v]));
}
}
}
return d[n] == 0x3f3f3f3f ? -1:d[n];
}
int main()
{
scanf("%d",&T);
int C = 1;
while(T--)
{
scanf("%d%d%d",&n,&m,&c);
top = 0;
memset(H,-1,sizeof(H));
for(int i=1;i<=n;i++)
{
int l;
scanf("%d",&l);
addedge(n+l,i,0);
addedge(i,2*n+l,0);
}
for(int i=2;i<=n;i++)
{
addedge(i-1+2*n,i+n,c);
addedge(i+2*n,i-1+n,c);
}
for(int i=1;i<=m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,c);
addedge(v,u,c);
}
printf("Case #%d: %d\n",C++,dij(1));
}
return 0;
}