【链接】http://acm.hdu.edu.cn/showproblem.php?pid=6181


【题意】


让你求从1到n的次短路

【题解】


模板题;
因为点可以重复走;
则一定会有次短路。
dijkstra算法+优先队列优化一下就好.

【错的次数】


11+

【反思】


在写优先队列的时候,节点和路径长度写反了..应该节点写在后面才行的。。

【代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb emplace_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
#define sz(x) ((int) x.size()) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; mt19937 myrand(time(0));
int get_rand(int n){return myrand()%n + 1;}
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5;
const LL INF = 0x3f3f3f3f3f3f3f3f; struct abc{
    int en,nex;
    LL w;
}; int n,m;
LL dis[N+10][2];
priority_queue <pll ,vector <pll> ,greater<pll> > dl;
vector <pii> G[N+10]; int main(){
    //Open();
    //Close();
    int T;
    ri(T);
    while (T--){
        ri(n),ri(m);
        rep1(i,1,n) G[i].clear();
        rep1(i,1,m){
            int a,b; LL w;
            ri(a),ri(b),rl(w);
            G[a].pb(mp(b,w));
            G[b].pb(mp(a,w));
        }
        ms(dis,INF);
        dis[1][0] = 0;
        dl.push(mp(0,1));
        while (!dl.empty()){
            LL d = dl.top().fi,x = dl.top().se;
            dl.pop();
            if (dis[x][1] < d) continue;
            int len = sz(G[x]);
            rep1(i,0,len-1){
                LL y = G[x][i].fi,cost = d + G[x][i].se;
                if (dis[y][0] > cost){
                    dis[y][1] = dis[y][0];
                    dis[y][0] = cost;
                    dl.push(mp(dis[y][0],y));
                }
                else if (dis[y][1] > cost){
                    dis[y][1] = cost;
                    dl.push(mp(dis[y][1],y));
                }
            }
        }
        ol(dis[n][1]);puts("");
    }
    return 0;
}

【hdu 6181】Two Paths的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  7. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  8. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

  9. 【HDU 4699】 Editor

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...

随机推荐

  1. MongoDB 基本使用

    数据库基本操作 连接到mongoDBserver  1 ./bin/mongo 127.0.0.1:12345  查看当前数据库 1 2 3 > show dbs admin  (empty) ...

  2. UVA 12716 GCD XOR(数论+枚举+打表)

     题意:给你一个N,让你求有多少组A,B,  满足1<= B <= A <= N, 且 gcd(A,B) = A XOR B. 思路:首先我们能够得出两个结论: A-B > ...

  3. Android LruCache 压缩图片 有效避免程序OOM

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9316683 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工, ...

  4. jquery点击弹框外层关闭弹框

    $(document).bind("click",function(e){            if($( e.target ).closest(".game-cont ...

  5. cogs 50. [NOIP2002] 选数

    50. [NOIP2002] 选数 ★   输入文件:choose.in   输出文件:choose.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述]: 已知 n 个整数 ...

  6. ArcGIS Engine能够做什么?

    转自原文ArcGIS Engine能够做什么? ArcGIS Engine是一组跨平台的嵌入式ArcObjects,它是ArcGIS软件产品的底层组件,用来构建定制的GIS和桌面制图应用程序,或是向原 ...

  7. 上下文切换查看 & sar

    怀疑CPU存在瓶颈,可用sar -u 和sar -q来看,怀疑I/O存在瓶颈,可用sar -b.sar -u和 sar-d来看 sar –W 查看页面交换发生状况 [root@localhost ~] ...

  8. C#变量引用与全局变量

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. Impala与HBase整合

    不多说,直接上干货! Impala可以通过Hive外部表方式和HBase进行整合,步骤如下: • 步骤1:创建hbase 表,向表中添加数据 create 'test_info', 'info' pu ...

  10. javaweb三、JDBC访问数据库

    JDBC是J2SE的内容,是由java提供的访问数据库的接口,但没有提供具体的实现方法,需要数据库厂商提供,就是对应的数据库驱动. 这样的好处是可以方便的更换数据库,提高了扩展性.这也是面向接口编程的 ...