HDU4725 The Shortest Path in Nya Graph【Dijkstra+思維】
The Shortest Path in Nya Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12241 Accepted Submission(s): 2641
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
Source
2013 ACM/ICPC Asia Regional Online —— Warmup2
Recommend
zhuyuanchen520
問題連結:HDU4725 The Shortest Path in Nya Graph
問題描述:給定n個點(從1開始編號),m條雙向邊,每個點屬於一個層次(1~n),一個層次可能包含多個點。第i層和第i+1層的結點可以相互訪問,距離為c,同一層的結點之間如果沒有邊相連就不能達,為從結點1到結點n的最短路徑
解題思路:問題的關鍵是如何處理相鄰層中的結點的相互訪問,如果結點個數不多,直接就讓第i層中的每個結點和第i+1層中的每個結點建立一條雙向邊,但是最多有10^5個結點,這樣會超記憶體,解決的辦法就是新增一些點,使得能實現相鄰層之間的相互訪問,如何新增點,見程式註釋,可以自己畫圖,這樣會更方便理解。
AC的C++程式:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int N=100100;
const int INF=0x3f3f3f3f;
int dist[3*N];//使用3*N是考慮了可能新增的結點
bool vis[3*N];//使用3*N是考慮了可能新增的結點
struct Edge{
int v,w;
Edge(int v,int w):v(v),w(w){}
};
struct Node{
int u,w;
Node(){}
Node(int u,int w):u(u),w(w){}
bool operator<(const Node &a)const
{
return w>a.w;
}
};
vector<Edge>g[3*N];//使用3*N是考慮了可能新增的結點
vector<int>layer[N];//layer[i]儲存在第i層的結點
void dijkstra(int s)
{
memset(dist,INF,sizeof(dist));
memset(vis,false,sizeof(vis));
priority_queue<Node>q;
dist[s]=0;
q.push(Node(s,0));
while(!q.empty()){
Node f=q.top();
q.pop();
int u=f.u;
if(!vis[u]){
vis[u]=true;
for(int i=0;i<g[u].size();i++){
int v=g[u][i].v;
if(!vis[v]&&dist[v]>dist[u]+g[u][i].w){
dist[v]=dist[u]+g[u][i].w;
q.push(Node(v,dist[v]));
}
}
}
}
}
int main()
{
int T,n,m,c,u,v,w,l;
scanf("%d",&T);
for(int t=1;t<=T;t++){
scanf("%d%d%d",&n,&m,&c);
for(int i=0;i<=n;i++)
layer[i].clear();
for(int i=0;i<=3*n;i++)//2n表示上次操作可能新增了2*n個結點
g[i].clear();
for(int i=1;i<=n;i++){
scanf("%d",&l);
layer[l].push_back(i);//第l層有結點i
}
while(m--){
scanf("%d%d%d",&u,&v,&w);
g[u].push_back(Edge(v,w));
g[v].push_back(Edge(u,w));
}
//層與層之間建邊
int node=n+1;//需要新增的點的編號
//按層數遞增的順序實現i能訪問i+1層:使用一個結點進行溝通
for(int i=1;i<n;i++)
if(!layer[i].empty()&&!layer[i+1].empty()){//如果第i層和第i+1層有結點
//第i層的各個結點到新增結點node的距離為c,結點node到第i+1層的結點的距離
//為0這樣就實現了第i層結點訪問第i+1層結點的距離為c
for(int j=0;j<layer[i].size();j++){
int u=layer[i][j];
g[u].push_back(Edge(node,c));
}
for(int j=0;j<layer[i+1].size();j++){
int v=layer[i+1][j];
g[node].push_back(Edge(v,0));
}
node++;
}
//按層數遞減的順序實現i訪問i-1層:使用一個結點進行溝通
for(int i=n;i>1;i--)
if(!layer[i].empty()&&!layer[i-1].empty()){//如果第i層和第i-1層有結點
//第i層的各個結點到新增結點node的距離為c,結點node到第i-1層的結點的距離
//為0這樣就實現了第i層結點到第i-1層結點的距離為c
for(int j=0;j<layer[i].size();j++){
int u=layer[i][j];
g[u].push_back(Edge(node,c));
}
for(int j=0;j<layer[i-1].size();j++){
int v=layer[i-1][j];
g[node].push_back(Edge(v,0));
}
node++;
}
dijkstra(1);
printf("Case #%d: ",t);
if(dist[n]==INF)
printf("-1\n");
else
printf("%d\n",dist[n]);
}
return 0;
}
相關文章
- POJ1847 Tram【Dijkstra+思維】
- [ABC362D] Shortest Path 3
- QOJ #8232. Yet Another Shortest Path Query
- 單源最短路徑(single-source shortest path )
- [ABC232G] Modulo Shortest Path (最佳化建圖)
- 題解:AT_abc362_d [ABC362D] Shortest Path 3
- CF 1805 D. A Wide, Wide Graph (*1800) 思維 + 樹的直徑IDE
- 思維體系---技術思維、業務資料思維、產品思維、複合思維
- 淺析工具思維、產品思維、品牌思維與定位
- 求職思維和招聘思維求職
- 把流量思維變成留量思維
- 框架思維框架
- 極思維
- 黑客思維黑客
- 你是整體思維還是分析思維? - kentbeck
- 計算思維
- 模型思維(01)模型
- 提升思維邏輯—SimpleMind Pro(思維導圖) for Mac/winMac
- 什麼是產品思維和專案思維? - Shreyas
- 《計算思維史話》思維導圖——持續更新
- 創新思維框架:第一原則思維 - Neil Kakkar框架
- 英語思維與物件導向分析思維的關係物件
- 運維工程師思維導圖運維工程師
- typora思維導圖
- 技術思維2
- 前端思維導圖前端
- Java思維理清思路Java
- HDU – 4811 – Ball (思維)
- 程式碼之外系列第一:索證思維與索進思維
- Xmind使用教程:給思維導圖加水印 「Xmind思維導圖2023」
- 思維決定命運,從四個方面理解深度思維的概念
- 什麼是Actor思維?
- 觀潮思維導圖
- 程式設計師思維程式設計師
- 思維導圖Xmind 2022
- 前端思維導圖 8前端
- B. Numbers Box(思維)
- 英語?思維導圖?