QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#737557#5145. Shortest PathIdtwteiWA 0ms6024kbC++142.6kb2024-11-12 16:12:252024-11-12 16:12:26

Judging History

你现在查看的是最新测评结果

  • [2024-11-12 16:12:26]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:6024kb
  • [2024-11-12 16:12:25]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define int long long
#define ar(x) array<int,x>
#define pb push_back
#define U(x) ((int)x.size())
const int N=2e3+100,INF=1e18,MOD=998244353;
inline void mod(int &x){ if(x>=MOD) x-=MOD; if(x<0) x+=MOD; }
inline int qm(int x,int y=MOD-2){ int res=1; for(;y;y>>=1,x=1ll*x*x%MOD) if(y&1) res=1ll*res*x%MOD; return res; }
inline int chkmin(int &x,int y){ return y<x?x=y,1:0; }
#define gc getchar()
#define rd read()
inline int read(){
	int x=0,f=0; char c=gc;
	for(;c<'0'||c>'9';c=gc) f|=(c=='-');
	for(;c>='0'&&c<='9';c=gc) x=(x<<1)+(x<<3)+(c^48);
	return f?-x:x;
}

int n,m,K,ans=0,inv2,f[N*4][N],g[N*4][N],F0[N],F1[N];
vector<ar(2)> G[N],odd,even;

inline int calc(ar(2) lin,int x){ return (lin[0]*x+lin[1]); }
ar(2) find(const vector<ar(2)> &vc,int x){
	int mn=INF; ar(2) res;
	for(auto [k,b]:vc) if(chkmin(mn,calc({k,b},x))) res={k,b};
	return res;
}

void solve(){
	n=rd,m=rd,K=rd,ans=0; for(int i=1,x,y,z;i<=m;++i) x=rd,y=rd,z=rd,G[x].pb({y,z}),G[y].pb({x,z});
	
	for(int i=0;i<=4*n+1;++i) for(int j=1;j<=n;++j) f[i][j]=g[i][j]=INF; f[0][1]=g[0][n]=0;
	for(int i=1;i<=4*n+1;++i) for(int u=1;u<=n;++u) for(auto [v,w]:G[u]) chkmin(f[i][v],f[i-1][u]+w),chkmin(g[i][v],g[i-1][u]+w);
	for(int i=1,mn;i<=n;++i){
		F0[i]=F1[i]=mn=INF;
		for(int j=0;j<=4*n+1;++j){
			if(j<=4*n) chkmin(F0[i],f[j][i]+g[4*n-j][i]);
			chkmin(F1[i],f[j][i]+g[4*n+1-j][i]);
		}
		for(auto [v,w]:G[i]){
			if(F0[i]!=INF) even.pb({w,F0[i]-4*n*w});
			if(F1[i]!=INF) odd.pb({w,F1[i]-(4*n+1)*w});
		}
	}
	
	for(int i=1;i<=4*n&&i<=K;++i) if(f[i][n]!=INF) mod(ans+=f[i][n]%MOD);
/*
	for(auto [k,b]:even){
		cout<<k<<" "<<b<<"\n";
	}
	puts("");
	for(auto [k,b]:odd){
		cout<<k<<" "<<b<<"\n";
	}
*/

	//even
	if(U(even)){
		for(int i=2*n+1,l,r;i*2<=K;i=l+1){
			l=i,r=K/2; auto [k,b]=find(even,i*2);
			while(l<r){
				int mid=l+r>>1;
				if(find(even,mid*2)[0]==k) l=mid+1;
				else r=mid-1;
			}
			mod(ans+=(calc({k,b},i*2)%MOD+calc({k,b},l*2)%MOD)*(l-i+1)%MOD*inv2%MOD);
		}
	}
	//odd
	if(U(odd)){
		for(int i=2*n,l,r;i*2+1<=K;i=l+1){
			l=i,r=(K-1)/2; auto [k,b]=find(odd,i*2+1);
			while(l<r){
				int mid=l+r>>1;
				if(find(odd,mid*2+1)[0]==k) l=mid+1;
				else r=mid-1;
			}
			mod(ans+=(calc({k,b},i*2+1)%MOD+calc({k,b},l*2+1)%MOD)*(l-i+1)%MOD*inv2%MOD);
		}	
	}
	
	printf("%lld\n", ans);
	
	for(int i=1;i<=n;++i) G[i]={}; odd={},even={};
}

signed main(){
	
	inv2=qm(2);
	
	int T=rd;
	while(T--) solve();
	
	return 0;
}

/*
4 6 1000000000
1 2 244
1 2 325
1 4 927
3 3 248
2 4 834
3 4 285
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 6000kb

input:

4
3 2 10
1 2 5
2 3 4
3 0 1000000000
3 3 100
1 2 3
1 3 4
2 3 5
4 6 1000000000
1 2 244
1 2 325
1 4 927
3 3 248
2 4 834
3 4 285

output:

125
0
15300
840659991

result:

ok 4 number(s): "125 0 15300 840659991"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 6024kb

input:

400
4 7 850187899
1 2 83
1 2 24
3 1 23
2 3 80
3 3 65
1 2 55
2 4 31
4 7 297586795
3 4 54
1 1 66
2 2 75
1 3 68
1 4 27
1 4 29
2 3 96
5 7 217277444
3 3 9
3 4 36
2 2 77
5 5 38
3 3 6
1 2 18
1 2 23
5 6 379032278
5 5 96
4 3 92
3 2 49
4 3 44
1 4 19
1 1 18
5 6 534284598
5 1 59
1 2 2
3 3 55
2 2 24
5 4 34
2 4 7...

output:

191476306
406722183
0
0
58483596
115858544
177378789
656019716
858116313
0
38761681
633010531
0
696693112
919354187
122684249
865794087
91541737
248634956
0
374201913
455984830
284991974
322357914
0
514668426
407812429
555256220
0
419773408
989291360
743372653
0
716008724
0
189418448
244106015
41273...

result:

wrong answer 1st numbers differ - expected: '191476186', found: '191476306'