Til the Cows Come Home POJ2387 (dijkstra+优先队列模版)

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<stack>
#include<cstdio>
#include<queue>
#include<functional>
#include<map>
#include<set>
#define se second
#define fi first
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define Pii pair<int,int>
#define pb push_back
#define ull unsigned long long
#define fio ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
const double pi=acos(-1.0);
const ull base=163;
const int N =4e3+5;
priority_queue<Pii,vector<Pii>,greater<Pii> >que;
int head[N];
int nx[N];
int to[N];
int len[N];
int tot=1;
int t,n;
int dis[N];
void add(int u,int v,int w){
    to[tot]=v;
    len[tot]=w;
    nx[tot]=head[u];
    head[u]=tot++;
}
void dij(){
    int INF=0x3f3f3f3f;
    fill(dis+1,dis+n+1,INF);
    que.push(make_pair(0,1));
    dis[1]=0;
    while(!que.empty()){
        Pii now=que.top();que.pop();
        int u=now.se;
        if(dis[u]<now.fi)continue;
        for(int i=head[u];i;i=nx[i]){
            int v=to[i];
            if(dis[v]>dis[u]+len[i]){
                dis[v]=dis[u]+len[i];
                que.push(make_pair(dis[v],v));
            }
        }
    }
}
int main(){
    fio;
    cin>>t>>n;
    for(int i=0;i<t;i++){
        int u,v,w;
        cin>>u>>v>>w;
        add(u,v,w);
        add(v,u,w);
    }
    dij();
    cout<<dis[n];
    return 0;
}

 

posted @ 2018-04-04 22:00  采蘑菇的小西佬  阅读(101)  评论(0编辑  收藏  举报