P - The Shortest Path in Nya Graph HDU - 4725

P - The Shortest Path in Nya Graph

HDU - 4725

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 <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), 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条边,每条边两点的距离为w。求1到n的最短路。

题解:这题建图很难,我开始用n方的方法建图,结果1e5的数据就超时了。后来网上看到一种方法,是把一个层看做一个点,比如第一层看作第n + 1个点,那么n + 1到属于第一层的距离的点为0,且为单向边(如果为双向边的话相同层的两点可以随意到达了),然后每层的点与相邻的层点相连,如2与n + 1、n + 3相连,单向边。这里好像不需要层点与层点相连,因为已经可以从普通点走向层点了,就不需要再经过层点到层点了。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1000100;
int dis[maxn],head[maxn],inq[maxn],a[maxn],n,m,c,tot;

struct edge
{
    int v;
    int w;
    int next;
}edg[maxn];

void addnode(int u,int v,int w)
{
    edg[tot].v = v;
    edg[tot].w = w;
    edg[tot].next = head[u];
    head[u] = tot++;
}

void SPFA()
{
    memset(dis,inf,sizeof(dis));
    memset(inq,0,sizeof(inq));

    queue<int>Q;
    Q.push(1);
    inq[1] = 1;
    dis[1] = 0;

    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        inq[u] = 0;
        for(int i = head[u];i != -1;i = edg[i].next)
        {
            int v = edg[i].v,w = edg[i].w;
            if(dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                if(!inq[v])
                {
                    Q.push(v);
                    inq[v] = 1;
                }
            }
        }
    }
}

int main()
{
    int t;
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        memset(head,-1,sizeof(head));
        tot = 0;

        scanf("%d %d %d",&n,&m,&c);
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&a[i]);
        }

        for(int i = 1;i <= n ;i++)
        {
            addnode(n + a[i],i,0);//层点通向该层的点建边
            if(a[i] > 1)
                addnode(i,n + a[i] - 1,c);//点与相邻层层点建边
            if(a[i] < n)
                addnode(i,n + a[i] + 1,c);
        }

        for(int i = 1;i <= m;i++)
        {
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);//点与点建边
            addnode(u,v,w);
            addnode(v,u,w);
        }

        SPFA();
        if(dis[n] == inf) printf("Case #%d: -1\n",++cas);
        else              printf("Case #%d: %d\n",++cas,dis[n]);

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Eric_chen_song_lin/article/details/82710761
今日推荐