1. 程式人生 > >The Shortest Path in Nya Graph HDU

The Shortest Path in Nya Graph HDU

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