1. 程式人生 > >luogu2865 [USACO06NOV]路障Roadblocks

luogu2865 [USACO06NOV]路障Roadblocks

dijkstra size 什麽 memset logs int down blog for

#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