Floyd
Floyd 本质上类似一种动态规划,dp [ i ] [ j ] = dp [ i ] [ k ] + dp[ k ] [ j ]。
/** * Night gathers, and now my watch begins. * It shall not end until my death. * I shall take no wife, hold no lands, father no children. * I shall wear no crowns and win no glory. * I shall live and die at my post. * I am the sword in the darkness. * I am the watcher on the walls. * I am the fire that burns against the cold, * the light that wakes the sleepers, * the shield that guards the realms of men. * I pledge my life and honor to the Night's Watch, * for this night, * and all the nights to come. */ #include<bits/stdc++.h> #define lson i<<2 #define rson i<<2|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define enld endl #define mian main #define itn int #define prinft printf const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; int Map[MAXN][MAXN]; int n, m, q; int a, b, c; void Floyd () { ; k < n; k++) ; i <= n; i++) ; j <= n; j++) Map[i][j] = min (Map[i][j], Map[i][k] + Map[k][j]); } int main() { while (cin >> n >> m >> q) { ; i <= n; i++) ; j <= n; j++) Map[i][j] = INF; ; i <= m; i++) { cin >> a >> b >> c; Map[a][b] = Map[b][a] = min (Map[a][b], c); } Floyd(); ; i <= q; i++) { cin >> a >> b; cout << Map[a][b] << endl; } } ; }
Floyd
/** * Night gathers, and now my watch begins. * It shall not end until my death. * I shall take no wife, hold no lands, father no children. * I shall wear no crowns and win no glory. * I shall live and die at my post. * I am the sword in the darkness. * I am the watcher on the walls. * I am the fire that burns against the cold, * the light that wakes the sleepers, * the shield that guards the realms of men. * I pledge my life and honor to the Night's Watch, * for this night, * and all the nights to come. */ #include<bits/stdc++.h> #define lson i<<2 #define rson i<<2|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define enld endl #define mian main #define itn int #define prinft printf const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; int Map[MAXN][MAXN]; int n, m, q; int a, b, c; void Floyd () { ; k <= n; k++) ; i <= n; i++) ; j <= n; j++) Map[i][j] = min (Map[i][j], Map[i][k] + Map[k][j]); } int main() { while (~scanf ("%d%d", &n, &m) && n || m) { ; i <= n; i++) ; j <= n; j++) Map[i][j] = INF; ; i <= m; i++) { scanf ("%d%d%d", &a, &b, &c); Map[a][b] = Map[b][a] = min (Map[a][b], c); } Floyd(); cout << Map[][n] << endl; } ; }
Dijkstra
从源点出发,首先寻找离源点最近的几个节点,now 储存现在离源点最近的节点的序号。
/** * Night gathers, and now my watch begins. * It shall not end until my death. * I shall take no wife, hold no lands, father no children. * I shall wear no crowns and win no glory. * I shall live and die at my post. * I am the sword in the darkness. * I am the watcher on the walls. * I am the fire that burns against the cold, * the light that wakes the sleepers, * the shield that guards the realms of men. * I pledge my life and honor to the Night's Watch, * for this night, * and all the nights to come. */ #include<bits/stdc++.h> #define lson i<<2 #define rson i<<2|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define enld endl #define mian main #define itn int #define prinft printf const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; int Map[MAXN][MAXN]; int dis[MAXN]; int vis[MAXN]; int n, m, q; int a, b, c; void Dijkstra (int src) { mem (vis, ); ; i <= n; i++) dis[i] = INF; dis[src] = ; ) { ; ; i <= n; i++) || dis[i] < dis[now])) now = i; ) break; vis[now] = ; ; i <= n; i++) dis[i] = min (dis[i], dis[now] + Map[now][i]); cout<<now<<endl; ; i <= n; i++) cout << dis[i] << ' '; cout << endl; } } int main() { while (cin >> n >> m >> q) { ; i <= n; i++) ; j <= n; j++) Map[i][j] = INF; ; i <= m; i++) { cin >> a >> b >> c; Map[a][b] = Map[b][a] = min (Map[a][b], c); } Dijkstra (); ; i <= q; i++) { cin >> a >> b; cout << dis[i] << endl; } } ; }
Dijkstra
/** * Night gathers, and now my watch begins. * It shall not end until my death. * I shall take no wife, hold no lands, father no children. * I shall wear no crowns and win no glory. * I shall live and die at my post. * I am the sword in the darkness. * I am the watcher on the walls. * I am the fire that burns against the cold, * the light that wakes the sleepers, * the shield that guards the realms of men. * I pledge my life and honor to the Night's Watch, * for this night, * and all the nights to come. */ #include<bits/stdc++.h> #define lson i<<2 #define rson i<<2|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define enld endl #define mian main #define itn int #define prinft printf const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; int Map[MAXN][MAXN]; int dis[MAXN]; int vis[MAXN]; int n, m, q; int a, b, c; void Dijkstra (int src) { mem (vis, ); ; i <= n; i++) dis[i] = INF; dis[src] = ; ) { ; ; i <= n; i++) || dis[i] < dis[now])) now = i; ) break; vis[now] = ; ; i <= n; i++) dis[i] = min (dis[i], dis[now] + Map[now][i]); cout<<now<<endl; ; i <= n; i++) cout << dis[i] << ' '; cout << endl; } } int main() { while (cin >> n >> m >> q) { ; i <= n; i++) ; j <= n; j++) Map[i][j] = INF; ; i <= m; i++) { cin >> a >> b >> c; Map[a][b] = Map[b][a] = min (Map[a][b], c); } Dijkstra (); ; i <= q; i++) { cin >> a >> b; cout << dis[i] << endl; } } ; }
SPFA
将每次被松弛了的节点入队,直到队列为空,得到的就是源点到各节点的最短路。
/** * Night gathers, and now my watch begins. * It shall not end until my death. * I shall take no wife, hold no lands, father no children. * I shall wear no crowns and win no glory. * I shall live and die at my post. * I am the sword in the darkness. * I am the watcher on the walls. * I am the fire that burns against the cold, * the light that wakes the sleepers, * the shield that guards the realms of men. * I pledge my life and honor to the Night's Watch, * for this night, * and all the nights to come. */ #include<bits/stdc++.h> #define lson i<<2 #define rson i<<2|1 #define LS l,mid,lson #define RS mid+1,r,rson #define mem(a,x) memset(a,x,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define enld endl #define mian main #define itn int #define prinft printf const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; ; ; ; ; using namespace std; //邻接表实现 struct node { int to, cost; node (int a, int b) { to = a, cost = b; } }; vector<node> edge[MAXN]; int vis[MAXN]; //可以用map int dis[MAXN]; int n, m; int a, b, c; queue<int> q; void spfa (int src) { mem (vis, ); vis[src] = ; dis[src] = ; q.push (src); while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = ; ; i < edge[now].size(); ++i) { if (dis[now] + edge[now][i].cost > dis[edge[now][i].to]) continue; dis[edge[now][i].to] = dis[now] + edge[now][i].cost; //更新 if (!vis[edge[now][i].to]) { //入队 q.push (edge[now][i].to); vis[edge[now][i].to] = ; } } } } int main() { while (cin >> n >> m && (n || m)) { while (!q.empty()) q.pop(); ; i <= n; i++) dis[i] = INF; ; i <= n; i++) edge[i].clear(); ; i <= m; i++) { cin >> a >> b >> c; edge[a].push_back (node (b, c)), edge[b].push_back (node (a, c)); } spfa (); cout << dis[n] << endl; } ; }
hdu 2544 SPFA