HDU-4725
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
题目描述
题目的意思是有N个点,分布在N层(一个层可以有多个点),相邻层一直的距离是C。另外有M条点到点的边,求1到N的最短路。
解题思路
普通的最短路好写(Dijkstra 两个for循环),这道题很特别它的点最多1e5,最坏的情况是一共有两层,每层50000个点。普通的建图方法(点到点的建图)就会超时,而且Dijkstra也会超时。
这是后就用到了优化队列版的Dijkstra和特殊的建图方式(我把它亲切的称为抽象层级建图)
代码实现
优先队列的Dijkstra也有两种形式优先队列+pair和优先队列+结构体
//优先队列 + pair
#include<bits/stdc++.h>
#define N 200005
//这里应该是2倍的范围,算上层数。一开始写成10005 给的结果是TLE.(WTF?),后来改成100005,给的是RTE。
#define P pair<int,int>
using namespace std;
struct ac{
int v,d;
};
vector<ac> ve[N];
void addedge(int u, int v, int c){
ac t;
t.v = v;
t.d = c;
ve[u].push_back(t);
}
int inf = 0x3f3f3f3f;
int n, m, c;
int dis[N], vis[N];
int ceng[N],visc[N];
void Dijkstra(){
memset(dis, inf, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[1] = 0;
priority_queue<P, vector<P>, greater<P> > que;
que.push(P(0, 1));
while(!que.empty()){
P p = que.top();
que.pop();
int v = p.second;
int d = p.first;
if(dis[v] < d || vis[v] == 1) //加快速度
continue;
if(v == n)
return ;
vis[v] = 1;
for(int i = 0; i < ve[v].size(); i++){
ac t; t = ve[v][i];
if(dis[t.v] > d + t.d){ //更新点并加入队列
dis[t.v] = d + t.d;
que.push(P(dis[t.v], t.v));
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
for(int j = 1; j <= t; j++){
cin >> n >> m >> c;
memset(visc, 0, sizeof(visc));
for(int i = 1; i <= n; i++){
cin >> ceng[i];
visc[ceng[i]] = 1; //标记出现的层数
}
for(int i = 1; i <= n; i++){
addedge(ceng[i] + n, i, 0);//层到层上的点距离为 0;为了区分点和层,层数加N
//640 MS
//不管3*7=21,直接连接上下层
// if(ceng[i] - 1 >= 1)
// addedge(i, ceng[i] + n - 1, c); //点指向下一层
// if(ceng[i] + 1 <= n)
// addedge(i, ceng[i] + n + 1, c); //点指向上一层
//624 MS
//增加一个visc数组,点与相邻且存在的层相连,避免Dijkstra在没有点的层上乱逛
if(ceng[i] - 1 >= 1 && visc[ceng[i] - 1])
addedge(i, ceng[i] + n - 1, c); //点指向下一层
if(ceng[i] + 1 <= n && visc[ceng[i] + 1])
addedge(i, ceng[i] + n + 1, c); //点指向上一层
}
for(int i = 0; i < m; i++){
int u, v, w;
cin >> u >> v >> w;
addedge(u, v, w);
addedge(v, u, w);
}
Dijkstra();
if(dis[n] == inf)
dis[n] = -1;
cout << "Case #" << j << ": " << dis[n] << "\n";
for(int i = 1; i <= n + n; i++){
ve[i].clear();
}
}
return 0;
}
//优先队列 + 结构体
#include<bits/stdc++.h>
#define N 200005
//这里应该是2倍的范围,算上层数。一开始写成10005 给的结果是TLE (WTF?),后来改成100005,给的是RTE。
#define P pair<int,int>
using namespace std;
struct ac{
int v,d;
//结构体重载操作
bool operator<(const ac &a)const{
return a.d < d;
}
};
vector<ac> ve[N];
void addedge(int u, int v, int c){
ac t;
t.v = v;
t.d = c;
ve[u].push_back(t);
}
int inf = 0x3f3f3f3f;
int n, m, c;
int dis[N], vis[N];
int ceng[N],visc[N];
void Dijkstra(){
memset(dis, inf, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[1] = 0;
priority_queue<ac > que;
ac t;
t.d = 0;
t.v = 1;
que.push(t);
while(!que.empty()){
t= que.top();
que.pop();
int v = t.v;
int d = t.d;
if(dis[v] < d || vis[v] == 1) //加快速度
continue;
if(v == n)
return ;
vis[v] = 1;
for(int i = 0; i < ve[v].size(); i++){
ac t; t = ve[v][i];
if(dis[t.v] > d + t.d){ //更新点并加入队列
dis[t.v] = d + t.d;
ac tt;
tt.d = dis[t.v];
tt.v = t.v;
que.push(tt);
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
for(int j = 1; j <= t; j++){
cin >> n >> m >> c;
memset(visc, 0, sizeof(visc));
for(int i = 1; i <= n; i++){
cin >> ceng[i];
visc[ceng[i]] = 1; //标记出现的层数
}
for(int i = 1; i <= n; i++){
addedge(ceng[i] + n, i, 0);//层到层上的点距离为 0;为了区分点和层,层数加N
//640 MS
//不管3*7=21,直接连接上下层
// if(ceng[i] - 1 >= 1)
// addedge(i, ceng[i] + n - 1, c); //点指向下一层
// if(ceng[i] + 1 <= n)
// addedge(i, ceng[i] + n + 1, c); //点指向上一层
//624 MS
//增加一个visc数组,点与相邻且存在的层相连,避免Dijkstra在没有点的层上乱逛
if(ceng[i] - 1 >= 1 && visc[ceng[i] - 1])
addedge(i, ceng[i] + n - 1, c); //点指向下一层
if(ceng[i] + 1 <= n && visc[ceng[i] + 1])
addedge(i, ceng[i] + n + 1, c); //点指向上一层
}
for(int i = 0; i < m; i++){
int u, v, w;
cin >> u >> v >> w;
addedge(u, v, w);
addedge(v, u, w);
}
Dijkstra();
if(dis[n] == inf)
dis[n] = -1;
cout << "Case #" << j << ": " << dis[n] << "\n";
for(int i = 1; i <= n + n; i++){
ve[i].clear();
}
}
return 0;
}