kuangbin_ShortPathP (HDU 4725)

很有挑战的一题 直接暴力建图的话毫无疑问O(n^2)会TLE 每层虚拟一个点又会让没有点的层也能连过去

参考kuangbin菊苣的方法每层用了两个虚拟点 n+i*2-1 是入口 n+i*2 是出口 然后建单向边就可以了

VA了一次 因为MAXN应该比数据量大两倍 不小心忽略了 至于MAXM直接开到了1e7

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std;

const int MAXN = 1e6 + 10;
const int MAXM = 2e7 + 10;

typedef pair<int, int> pii;
struct cmp{
    bool operator ()(const pii a, const pii b){
        return a.first > b.first;
    }
};

int size, head[MAXN], point[MAXM], nxt[MAXM], val[MAXM];
int t, n, m, c, dist[MAXN];

void init()
{
    size = 0;
    memset(head, -1, sizeof head);
}

inline void add(int from, int to, int value)
{
    val[size] = value;
    point[size] = to;
    nxt[size] = head[from];
    head[from] = size++;
}

void dij(){
    priority_queue<pii, vector<pii>, cmp> q;
    memset(dist, 0x3f, sizeof dist);
    q.push(make_pair(0, 1));
    dist[1] = 0;
    while(!q.empty()){
        pii u = q.top();
        q.pop();
        if(u.first > dist[u.second]) continue;
        for(int i = head[u.second]; ~i; i = nxt[i]){
            if(dist[point[i]] > dist[u.second] + val[i]){
                dist[point[i]] = dist[u.second] + val[i];
                q.push(make_pair(dist[point[i]], point[i]));
            }
        }
    }
}

int main()
{
    scanf("%d", &t);
    for(int kase = 1; kase <= t; kase++){
        init();
        scanf("%d%d%d", &n, &m, &c);
        for(int i = 1; i <= n; i++){
            int layer;
            scanf("%d", &layer);
            add(n + 2*layer - 1, i, 0);
            add(i, n + 2*layer, 0);
        }
        for(int i = 1; i < n; i++){
            add(n + 2*i, n + 2*(i+1) - 1, c);
            add(n + 2*(i+1), n + 2*i - 1, c);
        }
        int u, v, w;
        for(int i = 1; i <= m; i++){
            scanf("%d%d%d", &u, &v, &w);
            add(u, v, w);
            add(v, u, w);
        }

        dij();
        printf("Case #%d: %d\n", kase, dist[n] == INF ? -1 : dist[n]);
    }
    return 0;
}
时间: 2024-08-25 14:43:54

kuangbin_ShortPathP (HDU 4725)的相关文章

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

http://acm.hdu.edu.cn/showproblem.php?pid=4725 求1-n最短路,每个点有一个层数,相邻层之间花费k可以到达 建图时把层数看成n个点,层到该层点距离为0,点到其相邻层距离为c,相邻层之间距离为c #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector> using namespa

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

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

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(构图)

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

2013 成都邀请赛

今年又要打邀请赛了,前段时间做比赛都被虐的够呛.感觉不会再爱了...所以挂了下去年的成都邀请赛的题目.发现简单题还是能切上几道的,可是难题就无能为力了.. .阿门,还是水平差得远啊.. . (ps:近期感觉状态不佳.依靠了队友神勇的发挥. .. Current Time: 2014-05-15 08:43:24 Contest Type: Public Start Time: 2014-05-14 15:10:00 Contest Status: Ended End Time: 2014-05-

最短路专题(不定期更新)

1.Til the Cows Come Home  POJ 2387 题意:找到从N到1的最短路径. 思路:SPFA. 1 #include<iostream> 2 #include<vector> 3 #include<queue> 4 using namespace std; 5 const int maxn = 1010; 6 const int INF = 0x7fffffff; 7 int n, t; 8 struct node 9 { 10 int to;

[kuangbin]带你飞之&#39;最短路练习&#39;专题(未完成)

// 带飞网址 ??•??•?? 专题四 最短路练习 √ POJ 2387 Til the Cows Come HomePOJ 2253 FroggerPOJ 1797 Heavy TransportationPOJ 3268 Silver Cow PartyPOJ 1860 Currency ExchangePOJ 3259 WormholesPOJ 1502 MPI MaelstromPOJ 3660 Cow ContestPOJ 2240 ArbitragePOJ 1511 Invitat

HDU 1058 Humble Numbers(离线打表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 解题报告:输入一个n,输出第n个质因子只有2,3,5,7的数. 用了离线打表,因为n最大只有5842. 1 #include<stdio.h> 2 #define INT __int64 3 INT ans[5850] = { 4 0,1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27,28,30,32,35,36,40,42,45,48,4