HDU4725 The Shortest Path in Nya Graph(堆优化的dijkstra算法)

题意:

这是一个非常容易解决的问题,您的任务只是计算图像,而仅是计算干草成本和算法成本。如果您不懂此段话,请继续。
Nya图是具有“层”的无向图。图中的每个节点都属于一个层,总共有N个节点。
您可以以成本C从x层中的任何节点移动到x + 1层中的任何节点,因为道路是双向的,因此也可以以相同的成本从x + 1层移动到x层。
此外,还有M个额外的边,每个边连接一对节点u和v,成本为w。
帮助我们计算从节点1到节点N的最短路径。

题解:

主要是建图。

N个点,然后有N层,要假如2*N个点。

总共是3*N个点。

点1~N就是对应的实际的点1~N.  要求的就是1到N的最短路。

然后点N+1 ~ 3*N 是N层拆出出来的点。

第i层,入边到N+2*i-1, 出边从N+2*i 出来。

N + 2*i    到  N + 2*(i+1)-1 加边长度为C. 表示从第i层到第j层。

N + 2*(i+1) 到 N + 2*i - 1 加边长度为C,表示第i+1层到第j层。

如果点i属于第u层,那么加边 i -> N + 2*u -1         N + 2*u ->i  长度都为0

不能用spfa算法搞,会超时,用堆优化的dijsktra算法。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=1e6+10;
const int inf=1e9;
int N,M,C;
struct node {
    int u;
    int v;
    int w;
    int next;
}edge[maxn];
int head[maxn];
int tol=0;
void addedge (int u,int v,int w) {
    edge[tol].u=u;
    edge[tol].v=v;
    edge[tol].w=w;
    edge[tol].next=head[u];
    head[u]=tol++;
}
int d[maxn];
int visit[maxn];
struct qnode {
    int v;
    int w;
    bool operator < (const qnode &r) const {
        return w>r.w;
    }
};
void dijkstra (int s) {
    memset(visit,0,sizeof(visit));
    for (int i=1;i<=N*3;i++) d[i]=inf;
    priority_queue<qnode> q;
    d[s]=0;
    q.push({s,0});
    qnode tmp;
    while (!q.empty()) {
        tmp=q.top();
        q.pop();
        int u=tmp.v;
        if (visit[u]) continue;
        visit[u]=1;
        for (int i=head[u];i!=-1;i=edge[i].next) {
            int v=edge[i].v;
            if (!visit[v]&&d[v]>d[u]+edge[i].w) {
                d[v]=d[u]+edge[i].w;
                q.push({v,d[v]});
            }
        }
    }
}
int main () {
    int T;
    scanf("%d",&T);
    for (int k=1;k<=T;k++) {
        scanf("%d%d%d",&N,&M,&C);
        memset(head,-1,sizeof(head));
        tol=0;
        for (int i=1;i<=N;i++) {
            int x;
            scanf("%d",&x);
            addedge(i,N+2*x-1,0);
            addedge(N+2*x,i,0);
        }
        for (int i=1;i<N;i++) {
            addedge(N+2*i-1,N+2*(i+1),C);
            addedge(N+2*(i+1)-1,N+2*i,C);
        }
        while (M--) {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        dijkstra(1);
        if (d[N]==inf) d[N]=-1;
        printf("Case #%d: %d\n",k,d[N]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zhanglichen/p/12523595.html

时间: 2024-08-10 06:58:02

HDU4725 The Shortest Path in Nya Graph(堆优化的dijkstra算法)的相关文章

HDU 4725 The Shortest Path in Nya Graph(构图)

The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13445    Accepted Submission(s): 2856 Problem Description This is a very easy problem, your task is just calculate

The Shortest Path in Nya Graph HDU - 4725

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 un

hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有n个点和n层,m条边,每一层的任意一个点都可以花费固定的值到下一层或者上一层的任意点 然后m条边链接的点可以花费给出的值进行转移,最后问从i点到n点最少要花费多少. 这题点的个数有100000,层数也是100000,不算额外边暴力建边肯定要爆. 所以可以把层数也当成一个点比如说i点在j层于是n+j就与i链接花费0然后i点可以和上下层任意一个点链接 及i与n+j+1链接i与n+j-1链接

HDU - 4725 (The Shortest Path in Nya Graph)层次网络

题意:有n个点,每个点都在一个层内,层与层之间的距离为c,一个层内的点可以到达与它相邻的前后两个层的点,还有m条小路 ..时间真的是卡的很恶心啊... 借一下别人的思路思路: 这题主要难在建图上,要将层抽象出来成为n个点(对应编号依次为n+1~n+n),然后层与层建边,点与点建边,层与在该层上的点建边(边长为0),点与相邻层建边(边长为c). ps:这样处理就不用拆点了.不过要注意的是相邻两层必须都要有点才建边(不然会WA,可以参考我贴的数据) 借鉴自:http://www.myexceptio

Heap+Dijkstra堆优化的Dijkstra

前面说到"原生的Dijkstra",由于Dijkstra采用的是贪心策略,在贪心寻找当前距离源结点最短的结点时需要遍历所有的结点,这必然会导致效率的下降,时间复杂度为n^n.因此当数据量较大时会消耗较长时间.为了提高Dijkstra的效率,只有对Dijkstra的贪心策略进行改进. 由于Dijkstra采用的贪心策略是每次寻找最短距离的结点并将其放入存放所有已知最短距离结点的S集合中,可以联想到堆以及优先级队列这些数据结构,这些结构都能非常高效地提供当前状态距离最短的结点.实践也可以证

Method for finding shortest path to destination in traffic network using Dijkstra algorithm or Floyd-warshall algorithm

A method is presented for finding a shortest path from a starting place to a destination place in a traffic network including one or more turn restrictions, one or more U-turns and one or more P-turns using a Dijkstra algorithm. The method as sets a

hihoCoder_#1109_堆优化的Prim算法

#1109 : 最小生成树三·堆优化的Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 回到两个星期之前,在成功的使用Kruscal算法解决了问题之后,小Ho产生了一个疑问,究竟这样的算法在稀疏图上比Prim优化之处在哪里呢? 提示:没有无缘无故的优化! 输入 每个测试点(输入文件)有且仅有一组测试数据. 在一组测试数据中: 第1行为2个整数N.M,表示小Hi拥有的城市数量和小Hi筛选出路线的条数. 接下来的M行,每行描述一条路线,其中第i行为3个整数N

POJ1797 Heavy Transportation (堆优化的Dijkstra变形)

Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place

hihoCoder #1109 最小生成树之堆优化的Prim算法

原题地址:http://hihocoder.com/problemset/problem/1109 #1109 : 最小生成树三·堆优化的Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 回到两个星期之前,在成功的使用Kruscal算法解决了问题之后,小Ho产生了一个疑问,究竟这样的算法在稀疏图上比Prim优化之处在哪里呢? 提示:没有无缘无故的优化! 输入 每个测试点(输入文件)有且仅有一组测试数据. 在一组测试数据中: 第1行为2个整数N.M,表示小