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

这是一个关于最短路径问题的编程挑战,描述了一个特殊的图结构——Nya Graph。题目给出了测试用例数量T,每个用例包含节点数N,额外边数M和相邻层间移动成本C。每个节点属于特定的层,并存在额外的边连接节点,具有特定的成本w。任务是求从节点1到节点N的最小成本路径。输出格式要求包含'Case #X:',并给出最小成本或在无解时输出-1。示例输入和输出展示了具体的数据格式和解题结果。
摘要由CSDN通过智能技术生成
 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

题意:一个建图方式特别的题,将层缩成一个源点 源点向层里所有的点连权为0的边就可以了 至于点到达相邻的层 ,将点连向相邻的层的源点。


#include<stdio.h>

#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;
#define INF  0x3f3f3f3f
#define MOD 1000000010
#define N  100010
int n, m, c;
int head[N * 10], pnt[N * 10];
int  nxt[N * 10], cost[N * 10], cnt;
void add_edge(int u, int v, int c)
{
    pnt[cnt] = v;
    cost[cnt] = c;
    nxt[cnt] = head[u];
    head[u] = cnt ++;
}

int d[2 * N], lay[N];
bool in[2 * N], have[N];

int spfa()
{
    queue<int> q;
    q.push(1);
    memset(d, 0x3f3f3f3f, sizeof d);
    memset(in, false, sizeof in);
    d[1] = 0;
    in[1] = true;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        in[u] = false;
        for(int i = head[u]; ~i ; i = nxt[i])
        {
            int v = pnt[i];
            if(d[v] > d[u] + cost[i])
            {
                d[v] = d[u] + cost[i];
                if(!in[v])
                {
                    q.push(v);
                    in[v] = true;
                }

            }
        }
    }
    return d[n];
}

int main()
{

    int t;
    scanf("%d", &t);
    int kase = 0;
    while(t--)
    {
        cnt = 0;
        memset(head, -1, sizeof head);
        memset(have, false, sizeof have);
        scanf("%d%d%d", &n, &m, &c);
        for(int i = 1; i <= n; ++i)
        {
            int x;
            scanf("%d", &x);
            lay[i] = x;
            have[x] = true;
        }
        for(int i = 1; i < n; ++i)    //层层 c双向边(也可以不连)
        {
            if(have[i] && have[i + 1])
            {
                add_edge(n + i, n + i + 1, c);
                add_edge(n + i + 1, n + i, c);
            }
        }
        for(int i = 1; i <= n; ++i)
        {
            add_edge(n + lay[i], i, 0);   //层和点之间0
            if(lay[i] > 1)
                add_edge(i, n + lay[i] - 1, c); //点和相邻层c
            if(lay[i] < n)
                add_edge(i, n + lay[i] + 1, c);
        }

        for(int i = 1; i <= m; ++i)
        {
            int x, y, w;
            scanf("%d%d%d", &x, &y, &w);
            add_edge(x, y, w);//无向图
            add_edge(y, x, w);
        }
        int ans = spfa();
        if(ans == INF) ans = -1;
        printf("Case #%d: %d\n", ++kase, ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值