The Shortest Path in Nya Graph HDU - 4725 增点建图 Dijkstra 邻接表
problem
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 <= 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(原题中的N应该为M), 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.
Examples
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个节点。
你可以从x层的任意节点移动到x + 1层的任意节点,花费C,因为道路是双向的,所以从x + 1层移动到x层的花费也是一样的。
此外,还有M条额外的边,每条边连接一对节点u和v,花费w。
帮助我们计算从节点1到节点N的最短路径,如果有输出最小值,否则输出-1.
题解
关键在于增点建图,因为题意俩个点之间可能有多个点相连,直接建图不行,做法是把每一层抽象成 一个点;
假设每层用一个点代替 ,用 层数+N的值 代表该层,例如一共有3个点,第一个点在第二层:层数(2)+N(3)=5;
最坏情况下每层有一个节点,那么最多有2*N个点,所以数组范围要开到2*N。
由于N范围为2e5,所以用邻接表而不能用邻接矩阵
建图方法和HDU5521类似,用一个点代表一层,但不同的是,HDU5521是【每一层中的所有点互通】;
本题只要求相邻层互通,注意:【每层之内是不允许互通的】;
因此建图需要注意:不能直接 【把新new的点 与层内的所有点直接建立双向边】
A 【建立 当前层的新new的虚拟节点 指向该层内的节点的单向边】【权值为0】
B、【第i层的节点指向 i-1层的虚拟节点的单向边】【权值为C】
C、【第i层的节点指向 i+1层的虚拟节点的单向边】【权值为C】
下面上图
对于第一组样例
3 3 3
1 3 2
1 2 1
2 3 1
1 3 3
按照原题意建图:
显然直接建图是不可以的
增点建图:
其中点4,5,6 为新增节点,分别代表第一,第二,第三层
对于第二组样例
3 3 3
1 3 2
1 2 2
2 3 2
1 3 4
增点建图:
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=2e5+10;
struct Node
{
int to,w;
Mode() {}
Node(int to,int w):to(to),w(w) {}
};
vector<Node> vv[maxn];//用vector表示邻接表
int N,M,C;
int layer[maxn],islayer[maxn];//layer记录第i点所在的层数,islayer记录该层是否有点
int vis[maxn];
int dist[maxn];
struct cmp
{
bool operator()(int a,int b)
{
return dist[a]>dist[b];
}
};
void Dijkstra()
{
priority_queue<int,vector<int>,cmp> pq;
fill(dist,dist+maxn,INF);
fill(vis,vis+maxn,0);
dist[1]=0;
pq.push(1);
while(!pq.empty())
{
int u=pq.top();
pq.pop();
if(vis[u])
continue;
vis[u]=1;
for(int i=0; i<vv[u].size(); i++)
{
int v=vv[u][i].to;
int w=vv[u][i].w;
if(dist[v]>dist[u]+w)
{
dist[v]=dist[u]+w;
pq.push(v);
}
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T,cas=0;
cin>>T;
while(T--)
{
memset(layer,0,sizeof(layer));
memset(islayer,0,sizeof(islayer));
memset(vv,0,sizeof(vv));
cin>>N>>M>>C;
//增点建图
for(int i=1; i<=N; i++)//为每层分配一个点,并把它与所在该层的点相连接
{
cin>>layer[i];
islayer[layer[i]]=1;
}
for(int i=1; i<=N; i++)//给每个点建边
{
vv[layer[i]+N].push_back(Node(i,0));
if(islayer[layer[i]-1])//判断上一层的点是否存在,若存在连接
{
vv[i].push_back(Node(layer[i]-1+N,C));
}
if(islayer[layer[i]+1])//判断下一层的点是否存在,若存在连接
{
vv[i].push_back(Node(layer[i]+1+N,C));
}
}
//插入M条边
for(int i=1; i<=M; i++)
{
int u,v,w;
cin>>u>>v>>w;
vv[u].push_back(Node(v,w));
vv[v].push_back(Node(u,w));
}
Dijkstra();
if(dist[N]!=INF)
cout<<"Case #"<<++cas<<": "<<dist[N]<<endl;
else
cout<<"Case #"<<++cas<<": -1"<<endl;
}
return 0;
}