Input line to ensure the presence of at least one track to store.
On a wave of http://acm.hdu.edu.cn/showproblem.php?pid=2544 code
#include <bits/stdc++.h> #include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <cmath> typedef long long ll; using namespace std; const int INT=1e6+5; #define lson rt<<1, l, m #define rson rt<<1|1, m+1, r #define read(x) scanf("%d",&x) #define lread(x) scanf("%lld",&x); #define pt(x) printf("%d\n",(x)) #define cn cin>> #define ct cout<< #define en <<endl #define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++) #define mem(s,t) memset(s,t,sizeof(s)) #define re return 0; string str1="CC",str2="PC",str3="PP",str4="CP"; int dp[101][101],dis[101],used[101]; int m,n,x,y,z; void Dijstra() { mem(used,0); rep(1,n) dis[i] = dp[1][i]; //距离初始化 dis[1] = 0; rep(1,n) { int index , mindis = INT; for(int j=1;j<=n;j++) { if( !used[j] && dis[j]<mindis ) { index = j; mindis = dis[j]; } } used[index] = 1; for(int j=1;j<=n;j++) { if( dis[index] + dp[index][j] < dis[j] ) dis[j] = dis[index] + dp[index][j]; } } ct dis[n] en; } void init() { while(cin>>n>>m&&(n&&m)) // n-m points edges { IF (n-== . 1 ) {CT 0 EN; Continue ;} // weight Initial maximized for ( int I = . 1 ; I <= n-; I ++ ) for ( int J = . 1 ; J <= n-; J ++ ) DP [I] [J] = the INT; // weight initialization REP ( . 1 , m) { CIN >> >> X >> Y Z; IF (DP [X] [Y ]> Z) DP [X] [Y]= dp[y][x] = z; } Dijstra(); /* rep(1,n) for(int j=1;j<=n;j++) dp[i][j] = dp[j][i] = i^j; */ } } int main() { init(); }
----------------------------------------------- separator - ---------------------------------------
Shortest do need to pay attention to the data range and optimize the point of (additional)
1.floyd algorithm commonly used words adjacency matrix calculation (matrix initialization data not too much)
2.dijkstra algorithm then optimized heap together, that is the priority queue (if the heap plus map, increase the delete operation will be faster)
Adapted to have n points, edges in FIG. M, given the starting point s, the shortest path can be determined arrival point of each other (if any)
3.SPFA algorithm, on the storm force wide search relaxation, a good mind to write pretty fast, in essence, BE optimization. Not available divergent view trellis diagram or the like
----------------------------------------------- separator - ---------------------------------------
Dijstra general idea is to use the algorithm:
1) maximizing the path (to maximize main role is to screen out some of the non-existent road)
2) the presence of the path input, initializes all unvisited point source and the distance to each point stored DIS [] array of the direct distance (dis storage source to the respective points)
Dijstra core code
3) one by one point judgment, find the shortest distance from each point source, and updates the state of the point
4)! ! ! (Knock on the blackboard)
if( dis[index] + dp[index][j] < dis[j] ) dis[j] = dis[index] + dp[index][j];
Update shortest distance
The figure below in conjunction with the above steps will be better understood (boo !!! figure is someone else's, not quiet)
1) to the weight of the edge
2) optimization stack
Input line to ensure the presence of at least one track to store.