【HDOJ】1706 The diameter of graph

这么个简单的题目居然没有人题解。floyd中计算数目,同时注意重边。

 /* 1706 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int INF = 0x3f3f3f3f;
const int maxn = ;
int dis[maxn][maxn];
int M[maxn][maxn];
int cnt[maxn][maxn];
int n, m; void floyd() {
rep(k, , n+) {
rep(i, , n+) {
if (M[i][k]>=INF || i==k)
continue;
rep(j, , n+) {
if (M[k][j]>=INF || k==j || i==j)
continue;
if (M[i][j] > M[i][k]+M[k][j]) {
M[i][j] = M[i][k] + M[k][j];
cnt[i][j] = cnt[i][k] * cnt[k][j];
} else if (M[i][j] == M[i][k]+M[k][j]) {
cnt[i][j] += cnt[i][k] * cnt[k][j];
}
}
}
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int u, v, w;
int mx;
int ans; while (scanf("%d %d", &n, &m)!=EOF) {
memset(M, 0x3f, sizeof(M));
memset(cnt, , sizeof(cnt));
rep(i, , m) {
scanf("%d %d %d", &u, &v, &w);
if (u == v)
continue;
if (M[u][v] > w) {
M[u][v] = M[v][u] = w;
cnt[u][v] = cnt[v][u] = ;
} else if (M[u][v] == w) {
++cnt[u][v];
++cnt[v][u];
}
}
floyd();
ans = ;
mx = -;
rep(i, , n+) {
rep(j, , i) {
if (M[i][j]==INF || i==j)
continue;
if (mx < M[i][j]) {
mx = M[i][j];
ans = cnt[i][j];
} else if (mx == M[i][j]) {
ans += cnt[i][j];
}
}
}
printf("%d %d\n", mx, ans);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
上一篇:超全面的JavaWeb笔记day21<过滤器>


下一篇:【MVVM Light】Messager的使用