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

题意:有n个点,每个点都在一个层内,层与层之间的距离为c,一个层内的点可以到达与它相邻的前后两个层的点,还有m条小路

。。时间真的是卡的很恶心啊。。。

借一下别人的思路思路:

这题主要难在建图上,要将层抽象出来成为n个点(对应编号依次为n+1~n+n),然后层与层建边,点与点建边,层与在该层上的点建边(边长为0),点与相邻层建边(边长为c)。

ps:这样处理就不用拆点了。不过要注意的是相邻两层必须都要有点才建边(不然会WA,可以参考我贴的数据)

借鉴自:http://www.myexception.cn/program/1403919.html

主要是把层也抽象为点

spfa:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 201000, INF = 0xfffffff;
int n,m,c;
struct node{
    int u, v, d, next;
}Node[maxn*20];
int d[maxn], vis[maxn], head[maxn], tmp[maxn], vv[maxn];
void add(int u,int v,int d,int i)
{
    Node[i].u = u;
    Node[i].v = v;
    Node[i].d = d;
    Node[i].next = head[u];
    head[u] = i;
}

void spfa(int s)
{
    queue<int> Q;
    mem(vis,0);
    fill(d,d+maxn,INF);
    d[s] = 0;
    Q.push(s);
    vis[s] = 1;
    while(!Q.empty())
    {
        int x = Q.front();Q.pop();
        vis[x] = 0;
        for(int i=head[x]; i!=-1; i=Node[i].next)
        {
            node e = Node[i];
            if(d[e.v] > d[x] + e.d)
            {
                d[e.v] = d[x] + e.d;
                if(!vis[e.v])
                {
                    Q.push(e.v);
                    vis[e.v] = 1;
                }
            }
        }
    }

}

int main()
{
    int T;
    int res = 0;
    scanf("%d",&T);
    while(T--)
    {
        int ans = 0;
        mem(tmp,0);
        mem(vv,0);
        mem(head,-1);
        scanf("%d%d%d",&n,&m,&c);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&tmp[i]);
            vv[tmp[i]] = 1;      //若这层有点 则标记
        }
        for(int i=1;i<=n;i++)
        {
            if(vv[i] && vv[i+1])     //判断相邻两层是否有点  若有 则连接相邻两层
            {
                add(n+i,n+i+1,c,ans++);
                add(n+i+1,n+i,c,ans++);
            }
        }
        for(int i=1;i<=n;i++)
        {
            add(n+tmp[i],i,0,ans++);   // 连接层与点
            if(tmp[i] > 1) add(i,n+tmp[i]-1,c,ans++);  //连接点与相邻层
            if(tmp[i] < n) add(i,n+tmp[i]+1,c,ans++);
        }
        for(int i=0; i<m; ++i)    //连接点与点
        {
            int u,v,d;
            scanf("%d%d%d",&u,&v,&d);
            add(u,v,d,ans++);
            add(v,u,d,ans++);
        }
        spfa(1);
        printf("Case #%d: ",++res);
        if(d[n]!=INF)
            printf("%d\n",d[n]);
        else
            printf("-1\n");

    }

    return 0;
}

原文地址:https://www.cnblogs.com/WTSRUVF/p/9128605.html

时间: 2024-10-31 14:29:30

HDU - 4725 (The Shortest Path in Nya Graph)层次网络的相关文章

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

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链接

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

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个点.

HDU - 2290 Find the Path(最短路)

HDU - 2290 Find the Path Time Limit: 5000MS   Memory Limit: 64768KB   64bit IO Format: %I64d & %I64u Submit Status Description Scofield is a hero in American show "Prison Break". He had broken the prison and started a big runaway. Scofield h

[LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径

An undirected, connected graph of N nodes (labeled?0, 1, 2, ..., N-1) is given as?graph. graph.length = N, and?j != i?is in the list?graph[i]?exactly once, if and only if nodes?i?and?j?are connected. Return the length of the shortest path that visits

Dijkstra’s Shortest Path Algorithm / LeetCode 787. Cheapest Flights Within K Stops

Dijkstra’s Shortest Path Algorithm 实现详见:https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-using-priority_queue-stl/ 需要注意的是,priority_queue并无法更新内部的元素,因此我们更新dist的同时,直接把新的距离加入pq即可.pq里虽然有outdated的dist,但是由于距离过长,他们并不会更新dist. // If there is sho

6-17 Shortest Path [4] (25分)

Write a program to find the weighted shortest distances from any vertex to a given source vertex in a digraph. If there is more than one minimum path from v to w, a path with the fewest number of edges is chosen. It is guaranteed that all the weights

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