题意:给定一个图,你家在0,让你找出到沿海的最短路径。
析:由于这个题最多才10个点,那么就可以用Floyd算法,然后再搜一下哪一个是最短的。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <stack> using namespace std; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 10 + 5; const int mod = 1e9; const char *mark = "+-*"; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; int n, m; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } inline int Min(int a, int b){ return a < b ? a : b; } inline int Max(int a, int b){ return a > b ? a : b; } int d[maxn][maxn]; int a[maxn]; int solve(){ for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) for(int k = 0; k < n; ++k) d[i][j] = Min(d[i][j], d[i][k]+d[k][j]); int ans = INF; for(int i = 0; i < n; ++i) if(a[i] == 1) ans = Min(ans, d[0][i]); return ans; } int main(){ while(scanf("%d", &n) == 1){ memset(a, -1, sizeof(a)); int x, y; for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) d[i][j] = INF; for(int i = 0; i < n; ++i){ scanf("%d %d", &m, &a[i]); for(int j = 0; j < m; ++j){ int u, w; scanf("%d %d", &u, &w); d[i][u] = w; } } int ans = solve(); printf("%d\n", ans); } return 0; }
时间: 2024-10-29 22:58:40