码迷,mamicode.com
首页 > 数据库 > 详细

luogu2865 [USACO06NOV]路障Roadblocks

时间:2017-12-02 12:59:11      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:names   node   jks   cpp   set   namespace   blog   str   ios   

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cassert>
using namespace std;
struct Edge{
    int too, nxt, val;
}edge[200005];
struct Node{
    int idd, val;
}d[25005];//开20000都会WA!不知道为什么
int n, r, hea[5005], dis[5005], sdi[5005], cnt, k=0, uu, vv, ww;
void add_edge(int fro, int too, int val){
    edge[++cnt].nxt = hea[fro];
    edge[cnt].too = too;
    edge[cnt].val = val;
    hea[fro] = cnt;
}
bool cmp(Node x, Node y){
    return x.val>y.val;
}
void dijkstra(){
    memset(dis, 0x3f, sizeof(dis));
    memset(sdi, 0x3f, sizeof(sdi));
    dis[1] = 0;
    Node ttt;
    ttt.idd = 1;
    ttt.val = 0;
    d[++k] = ttt;
    while(k){
        Node j=d[1];assert(j.idd<=5000);//断言是调试时候的
        pop_heap(d+1, d+1+k, cmp);
        k--;
        if(j.val>sdi[j.idd])    continue;
        for(int i=hea[j.idd]; i; i=edge[i].nxt){
            int t=edge[i].too;
            int tmp=j.val+edge[i].val;
            if(dis[t]>tmp){
                swap(dis[t], tmp);
                ttt.idd = t;
                ttt.val = dis[t];
                d[++k] = ttt;
                push_heap(d+1, d+1+k, cmp);
            }
            if(dis[t]<tmp && sdi[t]>tmp){
                sdi[t] = tmp;
                ttt.idd = t;
                ttt.val = sdi[t];
                d[++k] = ttt;
                push_heap(d+1, d+1+k, cmp);
            }
        }
    }
}
int main(){
    cin>>n>>r;
    memset(hea, 0, sizeof(hea));
    for(int i=1; i<=r; i++){
        scanf("%d %d %d", &uu, &vv, &ww);
        add_edge(uu, vv, ww);
        add_edge(vv, uu, ww);
    }
    dijkstra();
    cout<<sdi[n]<<endl;
    return 0;
}

luogu2865 [USACO06NOV]路障Roadblocks

标签:names   node   jks   cpp   set   namespace   blog   str   ios   

原文地址:http://www.cnblogs.com/poorpool/p/7953785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!