QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#592033 | #8135. Minimum Cost Flow² | 2020HZ06 | WA | 1ms | 4016kb | C++14 | 1.9kb | 2024-09-26 20:11:51 | 2024-09-26 20:11:51 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const ll mod=998244353;
int t,n,m;
int h[105],cnt,dfn[105],tim,f[105];
struct edge{
int to,c,nxt;
}e[605];
bool tr[605],vis[105];
ll a[305][305],ans[305];
ll qp(ll x,ll y){
ll r=1;
while(y){
if(y&1) r=r*x%mod;
x=x*x%mod,y>>=1;
}return r;
}
void add(int x,int y,int c){
++cnt;e[cnt]={y,c,h[x]},h[x]=cnt;
}
void dfs(int u){
dfn[u]=++tim;vis[u]=1;
for(int i=h[u];i;i=e[i].nxt){
if(vis[e[i].to]) continue;
tr[i]=tr[i^1]=1;f[e[i].to]=i;
dfs(e[i].to);
}
}
void gauss(){
for(int i=1;i<=m;i++){
ll mx=0,wz=-1;
for(int j=i;j<=m;j++)
if(a[j][i]>mx) mx=a[j][i],wz=j;
swap(a[wz],a[i]);
ll inv=qp(a[i][i],mod-2);
for(int j=i+1;j<=m;j++){
ll p=a[j][i]*inv%mod;
for(int k=i;k<=m+1;k++)
(a[j][k]+=mod-p*a[i][k]%mod)%=mod;
}
for(int j=i;j<=m+1;j++) a[i][j]=a[i][j]*inv%mod;
}
ans[m]=a[m][m+1];
for(int i=m-1;i>=1;i--){
for(int j=i;j>=1;j--) (a[j][m+1]+=mod-a[j][i+1]*ans[i+1]%mod)%=mod;
ans[i]=a[i][m+1];
}
}
int main(){
int x,y,c,line;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);line=n-1;
for(int i=1;i<=n;i++) h[i]=vis[i]=0;
memset(tr,0,sizeof(tr));
cnt=1;tim=0;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&c);
add(x,y,c),add(y,x,c);
}
for(int i=1;i<=m;i++)
for(int j=1;j<=m+1;j++) a[i][j]=0;
a[1][m+1]=1;
dfs(1);
for(int i=1;i<=m;i++){
int j=i*2,y=e[j].to,x=e[j^1].to;
if(x<n) a[x][i]=1;
if(y<n) a[y][i]=mod-1;
if(!tr[j]){
++line;a[line][i]=e[j].c;
if(dfn[x]<dfn[y]) swap(x,y),a[line][i]=-a[line][i];
while(x!=y){
a[line][f[x]/2]=(f[x]&1?mod-1:1)*e[f[x]].c;
x=e[f[x]^1].to;
}
}
}
gauss();
ll sum=0;
for(int i=1;i<=m;i++) (sum+=ans[i]*ans[i]%mod*e[i*2].c%mod)%=mod;
printf("%lld\n",sum);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3956kb
input:
4 4 4 1 2 1 2 4 1 1 3 1 3 4 1 3 3 1 2 1 1 3 1 2 3 1 5 6 1 2 1 2 3 3 2 4 1 3 4 1 3 5 1 1 5 8 4 5 1 2 1 1 3 2 2 3 1 3 4 1 4 2 8
output:
1 665496236 713031683 614304219
result:
ok 4 number(s): "1 665496236 713031683 614304219"
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 4016kb
input:
18 3 3 3 1 716147853 2 1 865756093 3 2 749398397 9 15 7 1 928709747 9 1 692128293 8 2 960581386 6 7 744136630 4 9 233596968 9 7 190944262 3 5 289260315 3 7 164971041 1 8 664146999 5 6 436111746 4 1 780866816 7 5 366276343 8 9 28381218 9 5 140872991 9 2 196864247 4 5 3 4 839263691 2 4 940408725 1 4 4...
output:
509847883 279046442 799909987 954553232 798224583 252566082 696231300 791580749 310547727 575449008 440135980 246650056 287050173 183566243 653404982 532887171 982600715 308326199
result:
wrong answer 4th numbers differ by non-multiple of MOD, - expected: '81894899', found: '954553232'