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
题目大意:
给出N个点(编号 1 到 N)和M条双向有权边,每条边连接着两个点,表示路径长度;每个点还有自己的一个层值,该点属于第几层;除了已给出的边可走之外,相邻两层可以从x层任意一点跨越到x+1层或x-1层任意一点,且花费为C(不是跨层就要花费C,而是选择跨层而不走已有连接路径时要花费C),问1号点到N号点的最短路。
分析:
要求最短路,根据题目数据,我们这里采用了堆优化的Dijkstra算法。关键是考虑如何建图。
这里有layer层的概念,所有在同一层中的点都可以走跨层的花费为c的路径。那么我们可以为每一层构造一个虚拟节点,来处理层的概念。这个虚拟节点到层内所有点的距离都为0。
要注意的是,这里的虚拟节点和层内点的边不能是无向边。因为题目中的要求是层内的点无法通过层的概念进行跨越(两点之间已有连接路径的情况并不是通过层的概念连接的),如果是无向边,会导致层内点可以通过虚拟节点相互无代价访问,与题意不合。
所以我们为每层建立两个虚拟节点,如下所示。
具体来说,我们为用i+n表示第i层的虚拟节点1,用于该层的入边,i+2n表示第i层的虚拟节点2,用于该层的出边。
建完图之后,直接以1为源点,跑一遍Dijkstra即可。
具体解释见代码。
//#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn=1e5+5;
const int INF=0x3f3f3f3f;
int vis[3*maxn],dis[3*maxn]; //注意这里要开三倍的空间
struct node{
int nxt,w;
};
int e,n,c;
vector<int> layer[maxn];
vector<node> gra[3*maxn]; //注意这里要开三倍的空间
struct point{
int val,id;
point(int id,int val):id(id),val(val) {}
bool operator <(const point &x)const{
return val>x.val;
}
};
void dijkstra(int s){
memset(vis,0,sizeof(vis));
for(int i=1; i<=3*n; i++)
dis[i]=INF;
priority_queue<point> q;
q.push(point(s,0));
vis[s]=1;
dis[s]=0;
while(!q.empty()){
int cur=q.top().id;
q.pop();
vis[cur]=1;
for(int i=0; i < gra[cur].size() ; i++){
int id=gra[cur][i].nxt;
int cost=gra[cur][i].w;
if(!vis[id]&&dis[id]>dis[cur]+cost){
dis[id]=dis[cur]+cost;
q.push(point(id,dis[id]));
}
}
}
}
int main(){
int t,tmp,u,v,w,cas=0;
scanf("%d",&t);
while(t--){
++cas;
scanf("%d%d%d",&n,&e,&c);
//初始化
for(int i=1;i<=n;i++){
layer[i].clear();
gra[i].clear();
gra[i+n].clear();
gra[i+2*n].clear();
}
//确定各个层内的节点
for(int i=1;i<=n;i++){
scanf("%d",&tmp);
layer[tmp].push_back(i);
}
//加入额外的固定无向边
for(int i=1;i<=e;i++){
scanf("%d%d%d",&u,&v,&w);
node nn;
nn.nxt=v;
nn.w=w;
gra[u].push_back(nn);
nn.nxt=u;
gra[v].push_back(nn);
}
for(int i=1;i<=n;i++){
for(int j=0;j<layer[i].size();j++){
node nn;
nn.w=0;
nn.nxt=layer[i][j];
gra[i+n].push_back(nn); //虚拟节点1(i+n)指向层中点,边权值为0
}
for(int j=0;j<layer[i].size();j++){
node nn;
nn.w=0;
nn.nxt=i+2*n;
gra[layer[i][j]].push_back(nn); //层中点指向虚拟节点2(i+2*n),边权值为0
}
//该层指向后一层的边
if(i!=n){
node nn;
nn.w=c;
nn.nxt=i+n+1;
gra[i+2*n].push_back(nn);
}
//该层指向前一层的边
if(i!=1){
node nn;
nn.w=c;
nn.nxt=i+n-1;
gra[i+2*n].push_back(nn);
}
}
//求最短路
dijkstra(1);
printf("Case #%d: %d\n",cas,dis[n]==INF?-1:dis[n]);
}
return 0;
}