P - The Shortest Path in Nya Graph HDU - 4725

本文介绍了一种解决特定图结构——Nya图中从节点1到节点N的最短路径问题的方法。Nya图包含层状结构,各层间移动成本固定,额外边连接不同节点。通过将层视为超级节点,建立特殊的图结构,利用SPFA算法求解最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

 

All-Pairs Shortest Path问题是指在一个带权有向图中,求出任意两个节点之间的最短路径。解决这个问题的算法称为All-Pairs Shortest Path算法。 常用的All-Pairs Shortest Path算法有Floyd-Warshall算法和Johnson算法。 Floyd-Warshall算法的基本思想是动态规划。用dist[i][j]表示从节点i到节点j的最短路径长度,用k表示中间节点,则有状态转移方程: ``` dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) ``` 其中,dist[i][j]的初始值为节点i到节点j的边权,如果i和j之间没有边,则为正无穷。算法的核心是对k从1到n的循环,依次更新dist[i][j]的值,最终得到所有节点之间的最短路径长度。 Floyd-Warshall算法的时间复杂度为O(n^3),其中n为节点数,主要时间花费在三层循环上,实际应用中可以通过空间换时间的方式优化算法。 Johnson算法的基本思想是通过引入一个虚拟节点,并将其与所有节点之间的边权设为0,将问题转化为带权有向图中的单源最短路径问题。然后使用Bellman-Ford算法求出虚拟节点到其它所有节点的最短路径长度,再用求最短路径时的松弛操作更新所有边的边权,将问题转化为带权有向图中的多源最短路径问题。最后使用Dijkstra算法求出所有节点之间的最短路径长度。 Johnson算法的时间复杂度为O(n^2logn+m),其中n为节点数,m为边数,主要时间花费在Bellman-Ford算法和Dijkstra算法上,实际应用中可以通过优化数据结构等方式优化算法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值