HDU 6181 Two Paths(dijk的深入)

41 篇文章 0 订阅
12 篇文章 0 订阅

这题真是弄的心态血炸..
虽然勉强写出来,但是要多回顾
题意:求一条与最短路!!(不同)!!的尽可能短的路.
先理解一下dijk.
他是每次用一个可以确定最短路的点,更新其他点的最短路.
那么我们想一想次短路,
假如是到n的次短路,和n相连的点有abc..
那么只可能是1到abc的最短路加abc到n的边,或者1到abc的最短路加他们的边.注意区分一下就好.

/*  xzppp  */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <tuple>
#include <set>
using namespace std;
#define debug(x) //std::cerr << #x << " = " << (x) << std::endl
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define MT make_tuple
#define PB push_back
typedef long long  LL;
typedef unsigned long long ULL;
typedef pair<LL,LL > pii;
//typedef pair<LL,LL> pll;
typedef pair<double,double > pdd;
typedef pair<double,int > pdi;
typedef tuple<LL,LL,LL> ti;
const int INF = 0x7fffffff;
const LL INFF = 0x7f7f7f7fffffffff;
const int MOD = 1e9+7;
const int MAXM = 1e5+17;
const int MAXN = 1e5+17;
struct edge
{
    LL to, cost;
    edge(LL a, LL b):to(a),cost(b){}
    edge(){}
}; 
vector<edge> G[MAXN];
vector<LL > ans;
LL d1[MAXN],d2[MAXN];
map <pii,int > mp;
int n,m;
bool cg;
void dijkstra(int s)
{
    priority_queue< pii, vector<pii>, greater<pii>  > que;
    for (int i = 0; i <= n; ++i)
    {
        d1[i] = INFF;
        d2[i] = INFF;
    }
    d1[s] = 0;
    que.push(pii(0, s));
    mp[pii(0,s)] = 1;
    while (!que.empty())
    {
        //cerr<<"asd"<<endl;
        pii p = que.top(); que.pop();
        int u = p.second;
        int k = mp[MP(p.first,u)];
        debug(u);
        //if (d1[v] < p.first) continue;
        for (int i = 0; i < G[u].size(); ++i)
        {   
            edge e = G[u][i];
            int v = e.to;
            if(k==1)
            {
                if(d1[v] > d1[u] + e.cost)
                {
                    d2[v] = d1[v];
                    que.push(pii(d1[v],v));
                    mp[pii(d1[v],v)] = 2;
                    d1[v] = d1[u] + e.cost;
                    que.push(pii(d1[v],v));
                    mp[pii(d1[v],v)] = 1;
                }
                else if(d1[v] == d1[u] + e.cost)
                {
                    cout<<d1[v]<<endl;
                    return;
                }
                else if(d2[v] > d1[u] + e.cost)
                {
                    d2[v] = d1[u] + e.cost;
                    que.push(pii(d2[v],v));
                    mp[pii(d2[v],v)] = 2;
                }
            }
            else
            {
                if(d2[v] > d2[u] + e.cost)
                {
                    d2[v] = d2[u] + e.cost;
                    que.push(pii(d2[v],v));
                    mp[pii(d2[v],v)] = 2;
                }
            }
        }
        for (int i = 0; i < 4; ++i)
        {
            debug(i);
            debug(d1[i]);debug(d2[i]);
        }
        //cerr<<endl;
    }
    //cout<<"Sdasd"<<endl;
    cout<<d2[n-1]<<endl;
}
int main(int argc, char const *argv[])
{    
    #ifdef GoodbyeMonkeyKing
        freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
    #endif
    int t,x;
    cin>>t;
    x = t;
    while(t--)
    {
        cin>>n>>m;
        for (int i = 0; i <= n; ++i)
        {
            G[i].clear();
        }
        for (int i = 0; i < m; ++i)
        {
            LL u,v,cost;
            scanf("%lld%lld%lld",&u,&v,&cost);
            u--;v--;
            G[u].push_back(edge(v,cost));
            G[v].push_back(edge(u,cost));
        }
        dijkstra(0);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值