HDU4725 The Shortest Path in Nya Graph(Dijkstra)

HDU4725 The Shortest Path in Nya Graph(Dijkstra)

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个点,相邻的两层距离是固定的c,有额外m条边,然后求1到n的最短路径,如果没有则输出-1。
把每一层都转换为点即可,把点都接上上下两层,距离为c。其余的就是一个Dijkstra板子题。

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<functional> 
#include<map>
//#include<unordered_map>
#define lowbit(x) ((x)&-(x));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10,NN=1e4+10,INF=0x3f3f3f3f,LEN=110;
const ll MOD=1e9+7;
const ull seed=31;
int n,m,c;
struct Edge{
	int next,to,dis;
}edge[N];
struct Node{
	int id,dis;
	Node(int id,int dis):id(id),dis(dis){}
	bool friend operator<(const Node &a,const Node &b){
		return a.dis>b.dis;
	}
};
int num_edge;
int head[N];
void add_edge(int from,int to,int dis){
	edge[++num_edge].next=head[from];
	edge[num_edge].to=to;
	edge[num_edge].dis=dis;
	head[from]=num_edge;
}
int dis[N];
bool done[N];
void dijkstra(){
	int s=1;
	for(int i=1;i<=2*n;i++){
		dis[i]=INF;
		done[i]=false;
	}
	dis[s]=0;
	priority_queue<Node>q;
	q.push(Node(s,0));
	while(!q.empty()){
		Node u=q.top();
		q.pop();
		if(done[u.id]) continue;
		done[u.id]=true;
		for(int i=head[u.id];i!=-1;i=edge[i].next){
			int v=edge[i].to,w=edge[i].dis;
			if(done[v]) continue;
			if(dis[v]>u.dis+w){
				dis[v]=u.dis+w;
				q.push(Node(v,dis[v]));
			}
		}
	}
	if(dis[n]!=INF) printf("%d\n",dis[n]);
	else printf("-1\n");
}
void init(){
	num_edge=0;
	memset(head,-1,sizeof head);
}
int main(){
	int t,T=0;
	scanf("%d",&t);
	while(t--){
		init();
		scanf("%d%d%d",&n,&m,&c);
		for(int i=1;i<=n;i++){
			int l;
			scanf("%d",&l);
			add_edge(n+l,i,0);
			add_edge(n+l+1,i,c);
			add_edge(i,n+l+1,c);
			if(l!=1){
				add_edge(n+l-1,i,c);
				add_edge(i,n+l-1,c);
			}
		}
		for(int i=1;i<=m;i++){
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			add_edge(u,v,w);
			add_edge(v,u,w);
		}
		printf("Case #%d: ",++T);
		dijkstra();
	}
}
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页