Find the shortest path from 1 to n
Pay attention to the heavy side run again Dijkstra.
1 /* ***********************************************2 Author:sunyuefeng3 Created TIME:2016/10/22 14:18:064 File name:a.cpp5 ************************************************ */6 7#include <cstdio>8#include <iostream>9#include <algorithm>Ten#include <cmath> One#include <cstring> A#include <string> -#include <bitset> -#include <map> the#include <Set> -#include <stack> -#include <vector> -#include <queue> +#include <list> - #defineM (A, B) memset (A,b,sizeof (a)) + using namespacestd; AtypedefLong Longll; at Const intinf=0x3f3f3f3f; - Const intmaxn=1e3+Ten; - Const intmod=1e7+7; - intdx[8]= {0,0,1,-1,1,-1,1,-1}; - intdy[8]= {1,-1,0,0,-1,1,1,-1}; - in intn,m; - intWAY[MAXN][MAXN]; to intDIS[MAXN]; + BOOLVIS[MAXN]; - the voidDijkstraintx) { * for(intI=1; i<=m;i++){ $dis[i]=inf;Panax Notoginsengvis[i]=false; - } thedis[x]=0; + for(intI=1; i<=m;i++){ A intk=-1; the intmin=inf; + for(intj=1; j<=m;j++){ - if(!vis[j]&&dis[j]<min) { $min=Dis[j]; $k=J; - } - } the if(k==-1) Break; -vis[k]=true;Wuyi for(intj=1; j<=m;j++){ the if(!vis[j]&&dis[j]>dis[k]+Way[j][k]) { -dis[j]=dis[k]+Way[j][k]; Wu } - } About } $ } - - intMain () - { A //freopen ("In.txt", "R", stdin); + //freopen ("OUT.txt", "w", stdout); the while(~SCANF ("%d%d",&n,&m)) { - inta,b,v; $ M (way,inf); the for(intI=0; i<n;i++){ thescanf"%d%d%d",&a,&b,&v); the if(way[a][b]>v) { theway[a][b]=v; -way[b][a]=v; in } the } theDijkstra1); About intmin=inf; theprintf"%d\n", Dis[m]); the } the return 0; +}
[An Ac a day ^_^] [Kuangbin take you to fly] topic four shortest short practice POJ 2387 Til the cows Come Home