https://www.acwing.com/problem/content/853/

一个点变小,其他点才会变小
队列queue存 变小的点;
需要用st标记
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
const int N=1e6+10;
int n,m;
int h[N],e[N],ne[N],w[N],idx;
int dist[N];
bool st[N];
void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

//一个点变小,点后面的才会变小
//队列里面存变小的节点;
int spfa(){
    memset(dist,0x3f,sizeof dist);
    dist[1]=0;
    queue<int> q;
    q.push(1);
    st[1]=true;
    while(q.size()){
        int t=q.front();q.pop();//t可以到达的点都可以更新;
        st[t]=false;
        for(int i=h[t];i!=-1;i=ne[i]){
            int j=e[i];
            if(dist[j]>dist[t]+w[i]){
                dist[j]=dist[t]+w[i];
                if(!st[j]){
                    st[j]=true;
                    q.push(j);
                }
            }
        }
    }

    return dist[n];
}

int main(){
    cin>>n>>m;
    memset(h,-1,sizeof h);
    for(int i=0;i<m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        add(a,b,c);
    }
   
    int k=spfa();
    if(k==0x3f3f3f3f) cout<<"impossible"<<endl;
    else cout<<k<<endl;
    return 0;

}
 posted on 2019-08-14 20:38  谁是凶手1703  阅读(39)  评论(0)    收藏  举报