HDU 4725 The Shortest Path in Nya Graph(最短路)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37943488/article/details/82622635

The Shortest Path in Nya Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11609    Accepted Submission(s): 2522


 

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个点,整个图分为几层,层与层之间有路径,一个层能到达它的上一层或下一层,额外还给出了一些边,问1到n的最短路

层与层之间建边,点与层之间建边

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
const int maxm=5e6+7;
const int inf=0x3f3f3f3f;
typedef pair<int,int> P;
struct Node
{
    int to;
    int len;
    int next;
}edge[maxm];
struct LDJ
{
	int index;
	int dis;
	bool operator <(const LDJ&xx)const
    {
        return dis>xx.dis;
    }
};
int cnt;
int head[maxn];
int layer[maxn];
int dis[maxn];
bool vis[maxn];
bool layer_vis[maxn];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(layer_vis,false,sizeof(layer_vis));
    return;
}
void add(int u,int v,int len)
{
	edge[cnt].to=v;
	edge[cnt].len=len;
	edge[cnt].next=head[u];
	head[u]=cnt++;
	return;
}
void dijkstra()
{
    priority_queue<LDJ> que;
    memset(dis,inf,sizeof(dis));
    memset(vis,false,sizeof(vis));
    dis[1]=0;
    LDJ start;
    start.index=1;
    start.dis=0;
    que.push(start);
    while(!que.empty())
    {
        LDJ now=que.top();
        que.pop();
        int node=now.index;
        if(vis[node]) continue;
        vis[node]=true;
        for(int i=head[node];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(!vis[v]&&dis[v]>dis[node]+edge[i].len)
			{
				dis[v]=dis[node]+edge[i].len;
				//vis[v]=true;
				LDJ tmp;
				tmp.index=v;
				tmp.dis=dis[v];
				que.push(tmp);
			}
		}
    }
    return;
}
int main()
{
    int test;
    scanf("%d",&test);
    for(int cas=1;cas<=test;cas++)
    {
        init();
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&layer[i]);
            layer_vis[layer[i]]=true;
        }
        for(int i=1;i<n;i++)
		{
			if(layer_vis[i]&&layer_vis[i+1])
			{
				add(n+i,n+i+1,k);
				add(n+i+1,n+i,k);
			}
		}
		for(int i=1;i<=n;i++)
		{
			add(n+layer[i],i,0);
			if(layer[i]>1)
			{
				add(i,layer[i]+n-1,k);
			}
			if(layer[i]<n)
			{
				add(i,layer[i]+n+1,k);
			}
		}
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        dijkstra();
        printf("Case #%d: %d\n",cas,dis[n]==inf?-1:dis[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/82622635