2014
02-10

最短路

2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0

3
2

floyd

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        hdu2544.cpp
*  Create Date: 2013-11-28 15:37:26
*  Descripton:  min path, floyd
*/

#include <cstdio>
#include <cstring>

const int MAXN = 110;
const int INF = 0x3c3c3c3c - 1;

int nn, en;	// num of node and edge
int map[MAXN][MAXN];

int min(int a, int b) {
return a < b ? a : b;
}

int floyd() {
for (int k = 1; k <= nn; k++)
for (int i = 1; i <= nn; i++)
for (int j = 1; j <= nn; j++)
map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
}

int main() {
int s, e, t;
while (~scanf("%d%d", &nn, &en) && (nn || en)) {
// init
for (int i = 1; i <= nn; i++)
for (int j = 1; j <= nn; j++)
map[i][j] = INF;
// input
for (int i = 0; i < en; i++) {
scanf("%d%d%d", &s, &e, &t);
if (t < map[s][e])
map[s][e] = map[e][s] = t;
}
floyd();
printf("%d\n", map[1][nn]);
}
return 0;
}

dijkstra

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        hdu2544.cpp
*  Create Date: 2013-11-28 15:37:26
*  Descripton:  min path, dijkstra
*/

#include <cstdio>
#include <cstring>

const int MAXN = 110;
const int INF = 0x3c3c3c3c - 1;

int nn, en;	// num of node and edge
int map[MAXN][MAXN], dis[MAXN];
bool vis[MAXN];

int dijkstra(int s, int e) {
memset(vis, 0, sizeof(vis));
// init the dis
for (int i = 1; i <= nn; i++)
if (i == s) dis[i] = 0;
else dis[i] = map[s][i];
vis[s] = true;
for (int i = 1; i <= nn; i++) {
int t = INF, k;
for (int j = 1; j <= nn; j++)
if (!vis[j] && t > dis[j])
t = dis[j], k = j;
if (t == INF) break;
vis[k] = true;
for (int j = 1; j <= nn; j++)
if (!vis[j] && dis[j] > dis[k] + map[k][j])
dis[j] = dis[k] + map[k][j];
}
return dis[e];
}

int main() {
int s, e, t;
while (~scanf("%d%d", &nn, &en) && (nn || en)) {
// init
for (int i = 1; i <= nn; i++)
for (int j = 1; j <= nn; j++)
map[i][j] = INF;
// input
for (int i = 0; i < en; i++) {
scanf("%d%d%d", &s, &e, &t);
if (t < map[s][e])
map[s][e] = map[e][s] = t;
}
printf("%d\n", dijkstra(1, nn));
}
return 0;
}

bellman ford

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        hdu2544.cpp
*  Create Date: 2013-11-28 15:37:26
*  Descripton:  min path, bellman ford
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 110;
const int INF = 0x3c3c3c3c - 1;

struct Edge {
int u, v;
int cost;
} e[MAXN * MAXN / 2];

int nn, en;	// num of node and edge
int dis[MAXN];

int bellman_ford(int op, int ed) {
for (int i = 1; i <= nn; i++)
dis[i] = INF;
dis[op] = 0;
for (int i = 0; i < nn - 1; i++)
for (int j = 0; j < en * 2; j++)
dis[e[j].v] = min(dis[e[j].v], dis[e[j].u] + e[j].cost);
return dis[ed];
}

int main() {
int op, ed, t;
while (~scanf("%d%d", &nn, &en) && (nn || en)) {
// input
for (int i = 0; i < en; i++) {
scanf("%d%d%d", &op, &ed, &t);
e[i].u = op; e[i].v = ed; e[i].cost = t;
e[i + en].u = ed; e[i + en].v = op; e[i + en].cost = t;
}
printf("%d\n", bellman_ford(1, nn));
}
return 0;
}

spfa+queue

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        hdu2544.cpp
*  Create Date: 2013-11-28 15:37:26
*  Descripton:  min path, spfa
*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;

const int MAXN = 110;
const int INF = 0x3c3c3c3c - 1;

int nn, en;	// num of node and edge
int map[MAXN][MAXN], dis[MAXN];
bool vis[MAXN]; // if in the queue

int spfa(int op, int ed) {
queue<int> q;
memset(vis, false, sizeof(vis));

for (int i = 1; i <= nn; i++)
dis[i] = INF;
dis[op] = 0;

q.push(op);
vis[op] = true;

while (!q.empty()) {
int cur = q.front();
q.pop();
vis[cur] = false;
for (int i = 1; i <= nn; i++)
if (dis[i] > dis[cur] + map[cur][i]) {
dis[i] = dis[cur] + map[cur][i];
if (!vis[i]) {
q.push(i);
vis[i] = true;
}
}
}
return dis[ed];
}

int main() {
int op, ed, t;
while (~scanf("%d%d", &nn, &en) && (nn || en)) {
// init
for (int i = 1; i <= nn; i++)
for (int j = 1; j <= nn; j++)
map[i][j] = INF;
// input
for (int i = 0; i < en; i++) {
scanf("%d%d%d", &op, &ed, &t);
if (t < map[op][ed])
map[op][ed] = map[ed][op] = t;
}
printf("%d\n", spfa(1, nn));
}
return 0;
}

1. 那个我知道，我是想问关于细节的，就是消除格式的，WP Limit Posts Automatically可以消除格式让文字的空间体积很稳定。现在的摘要斩断带有格式，这就让一些换行较多的文章让格式显得很乱。再次感谢你的回复以及超级强大的插件

2. 有限自动机在ACM中是必须掌握的算法，实际上在面试当中几乎不可能让你单独的去实现这个算法，如果有题目要用到有限自动机来降低时间复杂度，那么这种面试题应该属于很难的级别了。

3. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮