Hdu2544 最短路【最短路】
http://acm.hdu.edu.cn/showproblem.php?pid=2544
不解释,最短路裸题
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> using namespace std; template <class T> void checkmin(T &t,T x) {if(x < t) t = x;} template <class T> void checkmax(T &t,T x) {if(x > t) t = x;} template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;} template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;} typedef pair <int,int> PII; typedef pair <double,double> PDD; typedef long long ll; #define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++) const int N = 111 , M = 20000; #define inf (1<<29) int n , m ; int dist[N]; bool vis[N]; struct Edge { int v , w , next; Edge () {} Edge(int v,int w,int next) : v(v),w(w),next(next) {} }edge[M]; int E , head[N]; void init() { E = 0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int w) { edge[E] = Edge(v,w,head[u]); head[u] = E++; edge[E] = Edge(u,w,head[v]); head[v] = E++; } queue <int> q; void spfa() { for(int i=1;i<=n;i++) dist[i] = inf , vis[i] = 0; while(!q.empty()) q.pop(); q.push(1); dist[1] = 0; while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for(int i=head[u];i!=-1;i=edge[i].next) { int v = edge[i].v; if(dist[u] + edge[i].w < dist[v]) { dist[v] = dist[u] + edge[i].w; if(!vis[v]) { vis[v] = 1; q.push(v); } } } } } int main() { while(~scanf("%d%d",&n,&m) && n+m) { init(); for(int i=0;i<m;i++) { int u , v , w; scanf("%d%d%d",&u,&v,&w); addedge(u , v , w); } spfa(); printf("%d\n" , dist[n]); } return 0; }

浙公网安备 33010602011771号