最短路 2 [HDU - 6714 ](dijkstra算法)
题目链接:https://vjudge.net/problem/HDU-6714
思路:
仔细分析可以得知: (w[i][j])为(i->j)的最短路径中不包含端点的最大编号节点(如果有多个最短路径,选择最大编号节点较小的那个。)
那么(w[i][j])可以在dijkstra算法过程中动态规划求出。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ' ', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
' : ' ');}}
const int maxn = 1005;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n, m;
struct node
{
int to;
ll val;
node() {}
node(int tt, ll vv) {
to = tt;
val = vv;
}
bool operator < (const node & b) const {
return val > b.val;
}
};
std::vector<node> e[maxn];
ll dis[maxn][maxn];
int w[maxn][maxn];
void addedge(int a, int b, ll v)
{
e[a].push_back(node(b, v));
e[b].push_back(node(a, v));
}
bool vis[maxn];
void init(int id, int n)
{
for (int i = 1; i <= n; ++i)
{
dis[id][i] = 1e18;
vis[i] = 0;
}
}
priority_queue<node> heap;
void dijkstra(int id)
{
int strat = id;
init(id, n);
dis[id][strat] = 0ll;
heap.push(node(strat, 0ll));
node temp;
while (!heap.empty())
{
temp = heap.top();
heap.pop();
if (vis[temp.to])
{
continue;
} else
{
vis[temp.to] = 1;
}
for (auto & x : e[temp.to])
{
if (dis[id][temp.to] + x.val < dis[id][x.to])
{
if (temp.to != id && temp.to != x.to)
w[id][x.to] = max(temp.to, w[id][temp.to]);
dis[id][x.to] = dis[id][temp.to] + x.val;
heap.push(node(x.to, dis[id][x.to]));
} else if (dis[id][temp.to] + x.val == dis[id][x.to])
{
if (temp.to != id && temp.to != x.to)
w[id][x.to] = min(w[id][x.to], max(w[id][temp.to], temp.to));
}
}
}
}
const int mod = 998244353;
int main()
{
#if DEBUG_Switch
freopen("C:\code\input.txt", "r", stdin);
#endif
//freopen("C:\code\output.txt","w",stdout);
int t;
t = readint();
while (t--)
{
n = readint();
m = readint();
repd(i, 1, n)
{
repd(j, 1, n)
w[i][i] = 0;
}
repd(i, 1, m)
{
int x, y, val;
x = readint();
y = readint();
val = readint();
addedge(x, y, val);
w[x][y] = w[y][x] = 0;
}
repd(i, 1, n)
{
dijkstra(i);
}
int ans = 0;
repd(i, 1, n)
repd(j, 1, n)
ans = (ans + w[i][j]) % mod;
printf("%d
", ans);
repd(i, 1, n)
e[i].clear();
}
return 0;
}