### 藍橋杯 算法訓練 最短路

1 2 -1
2 3 -1
3 1 2

-2

`//最短路徑#include<iostream>#include<cstdio>#include<vector>using namespace std;const int N=20002;const int INF=10000000;vector<int> map[N];vector<int> maplen[N];//這個跟map[N]相結合構成一個圖int dist[N];int vis[N]={0};int di[N];int v=0;int n,m;void dijkstra(int k){int i,j,p,smin;for(i=0;i<map[k].size();i++){dist[map[k][i]]=maplen[k][i];di[v++]=map[k][i];}dist[k]=0;vis[k]=1;for(p=1;p<=n;p++){smin=INF;j=-1;int kj;for(i=0;i<v;i++){if(vis[di[i]]==0&&dist[di[i]]<smin){j=di[i];smin=dist[di[i]];}}if(j==-1) break;else{//di.erase(itr+kj);vis[j]=1;for(i=0;i<map[j].size();i++){if(vis[map[j][i]]==0&&(smin+maplen[j][i]<dist[map[j][i]])){if(dist[map[j][i]]==INF) di[v++]=map[j][i];dist[map[j][i]]=smin+maplen[j][i];}}}}}int main(){//freopen("zuiduan1.in","r",stdin);scanf("%d%d",&n,&m);int i;for(i=1;i<=n;i++){dist[i]=INF;}int ai,aj,ad;for(i=0;i<m;i++){scanf("%d%d%d",&ai,&aj,&ad);map[ai].push_back(aj);maplen[ai].push_back(ad);//cout<<ai<<" "<<aj<<" "<<ad<<endl;}dijkstra(1);for(i=2;i<=n;i++){cout<<dist[i]<<endl;}return 0;}`