HDU4725 The Shortest Path in Nya Graph【Dijkstra+思維】

Enjoy_process發表於2018-10-21

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;
}

 

相關文章