QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#469243 | #6894. Circuit | grass8cow | AC ✓ | 658ms | 4960kb | C++17 | 1.4kb | 2024-07-09 16:40:10 | 2024-07-09 16:40:11 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,m;
int wi[510][510];
const int I=1e9+10,mod=998244353;
#define ll long long
const ll I2=1e18;
ll dis[510];
int fh[510];
bool vis[510];
void ad(int &x,int y){x+=y;if(x>=mod)x-=mod;}
void sol(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)wi[i][j]=I;
for(int i=1,u,v,w;i<=m;i++)
scanf("%d%d%d",&u,&v,&w),wi[u][v]=w;
ll ans=1e18;int ans2=0;
for(int s=1;s<n;s++){
for(int i=1;i<=n;i++)dis[i]=1e18,fh[i]=0,vis[i]=0;
dis[s]=0,fh[s]=1;
for(int st=1;st<=n-s+1;st++){
int u=-1;
for(int i=s;i<=n;i++)if(!vis[i]&&(u==-1||dis[u]>dis[i]))u=i;
if(dis[u]>=I2)continue;
vis[u]=1;
for(int i=s;i<=n;i++)if(wi[u][i]!=I){
if(dis[i]>dis[u]+wi[u][i])
dis[i]=dis[u]+wi[u][i],fh[i]=fh[u];
else if(dis[i]==dis[u]+wi[u][i])
ad(fh[i],fh[u]);
}
}
for(int i=s+1;i<=n;i++)if(wi[i][s]!=I&&dis[i]<I2){
if(dis[i]+wi[i][s]<ans)
ans=dis[i]+wi[i][s],ans2=fh[i];
else if(dis[i]+wi[i][s]==ans)
ad(ans2,fh[i]);
}
}
if(ans==I2)puts("-1 -1");
else printf("%lld %d\n",ans,ans2);
}
int main(){
int T;scanf("%d",&T);while(T--)sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 658ms
memory: 4960kb
input:
13 3 4 1 2 4 2 1 3 2 3 2 3 1 1 10 12 1 2 1 2 3 1 3 4 1 4 1 1 4 5 1 5 6 1 6 7 1 7 4 1 3 8 1 8 9 1 9 10 1 10 3 1 2 1 1 2 1 12 20 1 2 3 2 1 3 1 3 2 3 2 1 1 4 1 4 5 1 5 2 1 1 6 1 6 2 2 1 7 1 7 2 2 1 8 1 8 2 2 1 9 1 9 2 2 2 10 1 10 1 2 2 11 1 11 12 1 12 1 1 500 249500 1 2 1 1 3 2 1 4 3 1 5 4 1 6 5 1 7 6 ...
output:
7 2 4 3 -1 -1 6 21 500 616118143 1002500000 124750 566 124750 2 124750 -1 -1 500098704 2 8500 207839182 116655401 5046 500000000000 1
result:
ok 13 lines