QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#762709#8232. Yet Another Shortest Path Query275307894aWA 667ms313748kbC++142.6kb2024-11-19 16:19:542024-11-19 16:19:55

Judging History

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

  • [2024-11-19 16:19:55]
  • 评测
  • 测评结果:WA
  • 用时:667ms
  • 内存:313748kb
  • [2024-11-19 16:19:54]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=1e6+5,M=N*4+5,K=1000+5,mod=1e9+7,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(28382);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#ifdef LOCAL
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
	#else 
	#define gdb(...) void()
	#endif
}using namespace Debug;
int n,m,k;
vector<pii> S1[N],S2[N],S[N];
vector<tuple<int,int,int>> Q[N];
int in[N],ti[N],ans[N],st[N*5],sh,g[N];
void addqry(int x,int y,int id,int w){
	for(auto [a,b]:S1[x]) for(auto [c,d]:S1[y]) if(a==c) ans[id]=min(ans[id],w+b+d);
	Q[x].emplace_back(y,id,w);Q[y].emplace_back(x,id,w);
}
void Solve(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int x,y,z;scanf("%d%d%d",&x,&y,&z);
		S[x].emplace_back(y,z);S[y].emplace_back(x,z);
		in[x]++;in[y]++;
	}
	queue<int> q;
	auto check=[&](int x){
		if(in[x]<=5&&in[x]>=0) q.push(x),in[x]=-1;
	};
	for(int i=1;i<=n;i++) check(i);
	for(int i=1;i<=n;i++){
		int x=q.front();q.pop();ti[x]=i;
		for(auto [a,b]:S[x]) in[a]--,check(a);
	}
	for(int i=1;i<=n;i++){
		for(auto [a,b]:S[i]) (ti[i]<ti[a]?S1[i]:S2[i]).emplace_back(a,b); 
	}
	scanf("%d",&k);
	for(int i=1;i<=k;i++){
		int x,y;scanf("%d%d",&x,&y);
		ans[i]=INF;
		addqry(x,y,i,0);
		for(auto [a,b]:S1[x]) addqry(a,y,i,b);
		for(auto [a,b]:S1[y]) addqry(a,x,i,b);
	}
	Me(g,0x3f);
	for(int i=1;i<=n;i++){
		st[++sh]=i;g[i]=0;
		for(auto [a,b]:S1[i]){
			g[a]=min(g[a],b);st[++sh]=a;
			for(auto [c,d]:S1[a]) g[c]=min(g[c],b+d),st[++sh]=c;
		}
		for(auto [x,y,w]:Q[i]) ans[y]=min(ans[y],g[x]+w);
		for(auto [a,b]:S2[i]) for(auto [c,d]:S1[a]){
			g[c]=min(g[c],b+d);st[++sh]=c;
			for(auto [e,f]:S1[c]) g[e]=min(g[e],b+d+f),st[++sh]=e;
		}
		for(auto [x,y,w]:Q[i]) if(!w) ans[y]=min(ans[y],g[x]+w);
		while(sh) g[st[sh--]]=INF;
	}
	for(int i=1;i<=k;i++) printf("%d\n",ans[i]>=INF?-1:ans[i]);
}
int main(){
	int t=1;
	// scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 12ms
memory: 110308kb

input:

6 9
1 2 4
2 3 6
3 6 5
6 5 3
5 4 2
4 1 3
3 4 9
1 3 100
5 3 1
5
1 3
1 6
3 4
3 5
2 5

output:

6
8
3
1
7

result:

ok 5 number(s): "6 8 3 1 7"

Test #2:

score: 0
Accepted
time: 8ms
memory: 110312kb

input:

6 4
1 2 1
2 3 1
3 4 1
4 5 1
3
1 4
1 5
1 6

output:

3
-1
-1

result:

ok 3 number(s): "3 -1 -1"

Test #3:

score: -100
Wrong Answer
time: 667ms
memory: 313748kb

input:

40005 79608
1 2 70031203
1 3 99924845
1 4 61645659
1 5 9324967
2 3 15761918
3 4 62534796
4 5 35260314
5 2 35948540
6 2 23727405
6 7 83302920
7 3 31010426
7 8 75060393
8 4 94275932
8 9 99663793
9 5 81701979
9 6 439297
10 6 46955645
10 11 89514237
11 7 21257310
11 12 53896253
12 8 67933315
12 13 26161...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

wrong answer 7165th numbers differ - expected: '10951309', found: '46786556'