HDU 4725 The Shortest Path in Nya Graph(优先队列优化 Dijkstra+建图)

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 <= 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
 
  
23 3 31 3 21 2 12 3 11 3 33 3 31 3 21 2 22 3 21 3 4
 

Sample Output
 
  
Case #1: 2Case #2: 3


题意:

第一行给出n,m,c,分别表示n个点,m条无向边,楼层之间的花费(可跳跃至相邻楼层中任意点)。

接下来一行n个数字,分别表示 每点得归属楼层。

接下来给出m行,无向边u,v,w,w代表权值。

求1到n得最小权值


思路:

点与点之间的路线比较好解决(直接建边)。

点与层之间建立如下边:

点 到 该点所在层出口,点所在层 到该点,各个出口到其相邻入口。(这样建图就不会,因为2点所在层一样犯瞬移的错误,和无法连跳两层的错误)

最短路采用 Dijkstra+优先队列 方法


代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

#define For(a,b,c) for(int a = b; a <= c; a++)
#define mem(a,b) memset(a,b,sizeof(a))
int first[300005], tot;

struct Node
{
    int v, w, next;
}e[600005];

struct Point
{
    int dis, x;
    Point(int a = 0, int b = 0): x(a), dis(b){}
    friend bool operator <(Point a, Point b)
    {
        return a.dis > b.dis;
    }
};

void add(int u, int v, int w)
{
    e[tot].v = v;
    e[tot].w = w;
    e[tot].next = first[u];
    first[u] = tot++;
}

int dis[300005];
bool book[300005];

int Dijkstra(int u, int n)
{
    mem(book,false);
    mem(dis,0x3f);
    dis[u] = 0;
    priority_queue<Point> q;
    q.push(Point(u,0));

    Point st;
    while(!q.empty())
    {
        st = q.top();
        q.pop();
        if(book[st.x]) continue;
        book[st.x] = true;
        for(int i = first[st.x]; ~i; i = e[i].next)
        {
            if(dis[e[i].v] > dis[st.x] + e[i].w)
            {
                dis[e[i].v] = dis[st.x] + e[i].w;
                q.push( Point(e[i].v, dis[e[i].v]) );
            }
        }
    }
    if(dis[n] == 0x3f3f3f3f) return -1;
    return dis[n];
}

int main()
{
    int T, cnt = 1;
    scanf("%d",&T);
    while(T--)
    {
        int N, M , C;
        scanf("%d%d%d",&N,&M,&C);
        mem(first,-1);
        tot = 0;
        int u, v, w;

        For(i,1,N)
        {
            scanf("%d",&u);
            add(i,u+N,0);//i->i的出口
            add(u+2*N,i,0);//i的入口->i
        }

        For(i,1,N)
        {
            //出口到相邻入口
            if(i < N) add(N+i,2*N+i+1,C);
            if(i > 1) add(N+i,2*N+i-1,C);
        }

        while(M--)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }

        printf("Case #%d: %d\n",cnt++,Dijkstra(1,N));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/j2_o2/article/details/79996894