题目:
1874 http://acm.hdu.edu.cn/showproblem.php?pid=1874
2444 http://acm.hdu.edu.cn/showproblem.php?pid=2544
裸单源最短路径。。。水题。。。
用很水的方法,floyd水果,复杂度n^3很吓人啊。。。。慢慢学各种最短路算法,这几题都要再用各种方法水一遍。。。
最近考试,伤不起呢。。。
1874:
#include <cstdio> #include <cstring> using namespace std; #define INF 10000 const int maxn = 201; int r[maxn][maxn]; int main() { int n, m; while (scanf("%d%d", &n, &m) != EOF) { memset (r, INF, sizeof(r)); int x, y, v; for (int i = 0; i < m; i++) { scanf("%d%d%d", &x, &y, &v); if (r[x][y] > v) r[x][y] = r[y][x] = v; }//for for (int i = 0; i < n ;i++) r[i][i] = 0; for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (r[i][k] + r[k][j] < r[i][j]) r[i][j] = r[j][i] = r[i][k] + r[k][j]; }//for scanf("%d%d", &x, &y); if (r[x][y] < INF) printf("%d\n", r[x][y]); else printf("-1\n"); }//while return 0; }//hehe
2444
#include <cstdio> #include <cstring> #define INF 0xfffff const int maxn = 1002; int p[maxn][maxn]; int main() { int n, m; int i, j, k; // freopen("in", "r", stdin); while (scanf("%d%d", &n, &m) && n && m) { for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) if (j == i) p[i][j] = p[j][i] = 0; else p[i][j] = p[j][i] = INF; int x, y, w; for (i = 0; i < m; i++) { scanf("%d%d%d", &x, &y, &w); if (p[x][y] > w) p[x][y] = p[y][x] = w; } for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) for (k = 1; k <= n; k++) if (p[i][k] + p[k][j] < p[i][j]) p[i][j] = p[j][i] = p[i][k] + p[k][j]; printf("%d\n", p[1][n]); } }