POJ 2387(迪杰斯特拉)

 

POJ2387

题目:裸体最短路,但是要的是n到1的最短路(没区别)

板子题注意先输入T后输入N

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector>
//const int maxn = 1e5+5;
#define ll long long
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

#define MAX INT_MAX
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
using namespace std;
const int N=100010,M=1000010;
int head[N],ver[N],edge[M],next[M],d[N];
int vis[N];
int n,m,tot;
priority_queue<pair  < int,int>  >que;
void add(int x,int y,int z)
{
    ver[++tot]=y,edge[tot]=z;
    next[tot]=head[x],head[x]=tot;
}
void dijkstra()
{
    memset(d,9999999,sizeof(d));
    memset(vis,0,sizeof(vis));
    d[n]=0;
    que.push(make_pair(0,n));
    while(que.size())
    {
        int x=que.top().second;
        que.pop();
        if(vis[x]==1) continue;
        vis[x]=1;

        for(int i=head[x];i;i=next[i])
        {
            int y=ver[i];
            int z=edge[i];
            if(d[y]>d[x]+z)
            {
                d[y]=d[x]+z;
                que.push(make_pair(-d[y],y));
            }
        }
    }
}
int main()
{
//   freopen("C:\\Users\\方瑞\\Desktop\\input.txt","r",stdin);
//    freopen("C:\\Users\\方瑞\\Desktop\\output.txt","w",stdout);
    cin>>m>>n;
    FOR(i,1,m)
    {
        int a,b,c;
        cin>>a>>b>>c;
        add(a,b,c);
        add(b,a,c);
    }
    dijkstra();
    cout<<d[1]<<endl;
//    FOR(i,1,n)
//    {
//        cout<<d[i]<<endl;
//    }


}

 

posted @ 2019-08-01 01:07  阿斯水生产线  阅读(240)  评论(0编辑  收藏  举报