HDU 4034 图论 Floyd

来源:互联网 发布:网页制作软件v 编辑:程序博客网 时间:2024/05/16 03:29
/*******************************************************************************   前几天真TMD NC死了。。。一次Floyd就完事,每次观察dp[i, k] + dp[k, j]和dp[i, j]的值,若dp[i, k] + dp[k, j] < dp[i, j],显然无解,因为尼玛的都不是最短路的图。。。如果dp[i, k] + dp[k, j] == dp[i, j],那就把i-->j这条边给删了,因为可以通过k为中间点走,所以多余了~*******************************************************************************/#include <iostream>#include <functional>#include <algorithm>#include <complex>#include <cstdlib>#include <cstring>#include <fstream>#include <iomanip>#include <sstream>#include <utility>#include <bitset>#include <cctype>#include <cstdio>#include <limits>#include <memory>#include <string>#include <vector>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <list>#include <map>#include <set>using namespace std;#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )#define CLR(x, k) memset((x), (k), sizeof(x))#define CPY(t, s) memcpy((t), (s), sizeof(s))#define SC(t, s) static_cast<t>(s)#define LEN(s) static_cast<int>( strlen((s)) )#define SZ(s) static_cast<int>( (s).size() )typedef double LF;typedef __int64 LL;//VCtypedef unsigned __int64 ULL;typedef pair<int, int> PII;typedef pair<LL, LL> PLL;typedef pair<double, double> PDD;typedef vector<int> VI;typedef vector<char> VC;typedef vector<double> VF;typedef vector<string> VS;template <typename T>T sqa(const T & x){return x * x;}template <typename T>T gcd(T a, T b){if (!a || !b){return max(a, b);}T t;while (t = a % b){a = b;b = t;}return b;};const int INF_INT = 0x3f3f3f3f;const LL INF_LL = 0x7fffffffffffffffLL;//15fconst double oo = 10e9;const double eps = 10e-7;const double PI = acos(-1.0);#define  ONLINE_JUDGEconst int MAXN = 104;int test, n, dp[MAXN][MAXN];bool hs[MAXN][MAXN];bool floyd(){for (int k = 0; k < n; ++k){for (int i = 0; i < n; ++i){if (i == k){continue ;}for (int j = 0; j < n; ++j){if (k == j || i == j){continue ;}if (dp[i][k] + dp[k][j] < dp[i][j]){return false;}if (dp[i][k] + dp[k][j] == dp[i][j]){hs[i][j] = false;}}}}return true;}void ace(){int cas = 1;int res = 0;for (cin >> test; test--; ++cas){cin >> n;for (int i = 0; i < n; ++i){for (int j = 0; j < n; ++j){cin >> dp[i][j];if (i == j){dp[i][j] = 0;}}}CLR(hs, true);for (int i = 0; i < n; ++i){for (int j = 0; j < n; ++j){if (0 == dp[i][j]){hs[i][j] = false;}}}cout << "Case " << cas << ": ";if (!floyd()){cout << "impossible" << endl;continue ;}res = 0;for (int i = 0; i < n; ++i){for (int j = 0; j < n; ++j){res += hs[i][j];}}cout << res << endl;}return ;}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);#endiface();return 0;}/*******************************************************************************Test Data...330 1 11 0 11 1 030 1 3 4 0 27 3 030 1 41 0 24 2 0*******************************************************************************/

原创粉丝点击