HDU --- 6181 Two Paths 2017第十场多校签到题【裸次短路】

传送门
// 题意就是求一个次短路, 多校居然出原题??? 直接套上POJ 3255 的代码都能过.
AC Code

/** @Cain*/
#define ll long long int
typedef pairint>P;
const ll INF = 1e18;
const int maxn = 1e5 + 5;
int cas = 1;
int head[maxn];
ll dis1[maxn],dis2[maxn];
int n,m,cnt;
struct node
{
    int to,next;
    ll w;
}s[maxn*2];

void add(int u,int v,ll w)
{
    s[cnt].to = v;
    s[cnt].w = w;
    s[cnt].next = head[u];
    head[u] = cnt++;
}
void solve()
{
    while(~scanf("%d%d",&n,&m)){
        if(n +m == 0) break;
        cnt = 0; Fill(head,-1);
        Fill(s,0);
        for(int i=1;i<=m;i++){
            int u,v;
            ll w;
            scanf("%d%d%lld",&u,&v,&w);
            add(u,v,w); add(v,u,w);
        }
        priority_queuevector

, greater

>q; for(int i=1;i<=n;i++) dis1[i] = dis2[i] = INF; dis1[1] = 0; q.push(P(0,1)); while(!q.empty()){ P tmp = q.top(); q.pop(); ll d = tmp.first; int u = tmp.second; if(d > dis2[u] ) continue; for(int i=head[u]; ~i ; i = s[i].next){ int to = s[i].to; ll d2 = d + s[i].w; if(dis1[to] > d2){ swap(dis1[to],d2); q.push(P(dis1[to],to)); } if(dis2[to] > d2 && dis1[to] < d2){ dis2[to] = d2; q.push(P(dis2[to],to)); } } } printf("%lld\n",dis2[n]); } }

你可能感兴趣的:(次短路)