您的位置:首页 > 其它

刷题—— hdu 6181 Two Paths

2017-08-24 20:01 323 查看
/*

裸的次短路

不断更新v->u的次短路,直到v->u的次短路只比最短路小

*/

#include <vector>

#include <iostream>

#include <stdio.h>

#include <queue>

using namespace std;

#define ll long long

#define INF 1e18

#define MAXM 100100

struct edge{int to;ll w;};

typedef pair<ll,int>P;

int n,r;

ll d[MAXM];

ll d1[MAXM];

vector<edge>G[MAXM];

bool vis[MAXM];

void solve(){

    priority_queue<P, vector<P>,greater<P> >que;

    fill(d+1,d+n+1,INF);

    fill(d1+1,d1+n+1,INF);

    d[1]=0;

    que.push(P(0,1));

    while(!que.empty(
4000
)){

        P p=que.top();

        que.pop();

        int v=p.second;

        if(d1[v]<p.first)continue;

        for(int i=0;i<G[v].size();i++){

            edge &e=G[v][i];

            ll d2=p.first+e.w;

            if(d[e.to]>d2){

                swap(d[e.to],d2);

                que.push(P(d[e.to],e.to));

            }

            if(d1[e.to]>d2&&d[e.to]<d2){

                d1[e.to]=d2;

                que.push(P(d1[e.to],e.to));

            }

        }

    }

    printf("%I64d\n",d1
);

}

int main(){

    int t;

    scanf("%d",&t);

    while(t--){

        scanf("%d %d",&n,&r);

        for(int i=1;i<=n;i++){

            G[i].clear();

        }

        int u,v;

        ll w;

        while(r--){

            scanf("%d %d %I64d",&u,&v,&w);

            G[u].push_back(edge{v,w});

            G[v].push_back(edge{u,w});

        }

        solve();

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: