Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103223#problem/B
This problem uses the classical Dijsktra algorithm, the approximate idea is to use the dist[x] store x to have determined the set of the shortest, n cycles to this minimum value, and then update the other points to the new set of the shortest path is the corresponding dist[]
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5 using namespacestd;6 Const intMAX = the;7 Const intINF =0x3f3f3f3f;8 intG[max][max],dist[max],vis[max];9 intn,m;Ten voidDijstra () One { Amemset (Vis,0,sizeof(Vis)); - for(inti =1; I <= N; i++) - { the if(g[1][i]! =INF) -Dist[i] = g[1][i]; - Else -Dist[i] =INF; + } -dist[1] =0; +vis[1] =1; A intMaxn,pos =1, temp;//pos = 1, as a starting point at for(inti =1; I <= N; i++) - { -MAXN =INF; - for(intj =1; J <= N; J + +) - { - if(Vis[j] = =0&& Dist[j] < MAXN)//The minimum distance for the collection could not be found, MAZN in { -MAXN =Dist[j]; totemp =J; + } - } theVis[temp] =1;//Add this point to the collection, which is set to access 1 *pos =temp; $ for(intj =1; J <= N; J + +)Panax Notoginseng { - if(Vis[j] = =0)//The distance from the new POS as the starting point to the collection theDist[j] = min (Dist[j], Dist[pos] +g[pos][j]); + } A } the } + intMain () - { $ while(SCANF ("%d%d", &n,&m)! =EOF) $ { - inta,b,c; - if(n = =0|| m = =0) the Break; -Memset (g, INF,sizeof(g));Wuyi for(inti =1; I <= m; i++) the { -scanf"%d%d%d", &a,&b,&c); Wu if(G[a][b] > C)//Consider the heavy side, go to the minimum -G[A][B] = G[b][a] =C; About } $ Dijstra (); -printf"%d\n", Dist[n]); - } - return 0; A}
View Code
Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103223#problem/C
Topology sorting is to find the degree of 0 points, found after the update of the other points in the degree, and then in the search for the point of 0 until there is no 0 point, in order to facilitate understanding here directly with the double loop write, late everyone familiar with the stack to deposit the degree of 0 points
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 intg[550][550];7 intindegree[550];//Deposit Degree8 intpur[550];9 intMain ()Ten { One intn,m,x,y; A while(SCANF ("%d%d", &n,&m)! =EOF) - { -memset (Indegree,0,sizeof(Indegree)); theMemset (G,0,sizeof(g)); - - for(inti =1; I <= M; i++) - { +scanf"%d%d",&x,&y); - if(G[x][y] = =0) + { AG[x][y] =1; atindegree[y]++; - } - } - intk=1; - for(inti =1; I <= N; i++) - { in for(intj =1; J <= N; J + +) - { to if(Indegree[j] = =0)//find a point with a degree of 0 + { -pur[k++] =J; theindegree[j]--; * for(intm =1; M <= N; m++)//Update $ {Panax Notoginseng if(G[j][m]) -indegree[m]--; the } + Break; A } the } + } - for(inti =1; I < n; i++) $printf"%d", Pur[i]); $printf"%d\n", Pur[n]); - } - return 0; the}
View Code
Looking for the first question did not notice the data type, finally may harm many people wasted a lot of time, sorry ... Graph theory topic may not see, should data structure everyone also talked about, but please remember, speak, understand, not necessarily really will, only AC is kingly!
20151230 Training (Shortest path + topological sort)