NC17696. Rikka with Line Graph
描述
输入描述
The first line contains a single number t(1 ≤ t ≤ 3), the number of the testcases.
For each testcase, the first line contains one single integer n(2 ≤ n ≤ 500).
Then n lines follow, each line contains n integers wi,j(0 ≤ wi,j ≤ 109), the weight of each edge in G. Since there are no self circles in G, the value of wi,i is meaningless.
The input guarantees that for all 1 ≤ i ≠ j ≤ n, wi,i=0 and wi,j = wj,i.
输出描述
For each testcase, output a single line with a single number, the answer modulo 998244353.
示例1
输入:
3 2 0 1 1 0 3 0 1 1 1 0 1 1 1 0 4 0 1 1 1 1 0 2 2 1 2 0 3 1 2 3 0
输出:
0 6 56
C++14(g++5.4) 解法, 执行用时: 8348ms, 内存消耗: 1388K, 提交时间: 2018-08-19 15:34:16
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn = 500 + 9; const int mod=998244353; int T, N; int w[maxn][maxn]; int dis[maxn]; ll inv2=(mod+1)>>1; int main() { scanf("%d",&T); while(T--) { scanf("%d", &N); ll ans=0; ll tim=N*(N-1)/2-1; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { scanf("%d", &w[i][j]); ans+=w[i][j]; } } ans=ans%mod*tim%mod*inv2%mod; for(int k = 0; k < N; k++) { for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { w[i][j] = min(w[i][j], w[i][k] + w[k][j]); } } } ll res=0; for(int i=0;i<N;++i) { ll ret=0; for(int j=i+1;j<N;++j) { for(int k=0;k<N;++k) dis[k]=-min(w[i][k],w[j][k]); sort(dis,dis+N); for(int k=0;k<N;++k) ret+=(ll)(-dis[k])*(ll)k; } res=(res+ret)%mod; } ans+=res; if (ans>=mod) ans-=mod; printf("%lld\n",ans); } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 9586ms, 内存消耗: 2660K, 提交时间: 2020-03-29 15:13:57
#include<bits/stdc++.h> using namespace std; #define go(i,a,b) for(int i=a;i<=b;i++) #define mod 998244353 #define ll long long #define N 555 ll w[N][N],m,n,t,ans,a[N]; int main() { scanf("%lld",&t); while(t--) { scanf("%lld",&n);m=n*(n-1)/2-1;ans=0; go(i,1,n)go(j,1,n)scanf("%lld",&w[i][j]); go(i,1,n)go(j,i+1,n)(ans+=w[i][j]*m%mod)%=mod; go(k,1,n)go(i,1,n)go(j,1,n)w[i][j]=min(w[i][j],w[i][k]+w[k][j]); go(i,1,n)go(j,i+1,n) { go(k,1,n)a[k]=min(w[i][k],w[j][k]);sort(a+1,a+1+n); go(k,1,n)(ans+=1ll*(n-k)*a[k]%mod)%=mod; } ans=(ans+mod)%mod;printf("%lld\n",ans); } return 0; }