QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#883121#8232. Yet Another Shortest Path QueryhotdogsellerTL 8968ms221020kbC++143.5kb2025-02-05 14:47:182025-02-05 14:47:26

Judging History

This is the latest submission verdict.

  • [2025-02-05 14:47:26]
  • Judged
  • Verdict: TL
  • Time: 8968ms
  • Memory: 221020kb
  • [2025-02-05 14:47:18]
  • Submitted

answer

#include<bits/stdc++.h>
#include <utility>

#define maxn 1000005
#define INF 1e9
#define pii pair<int,int>

using namespace std;

inline long long read(){
	long long lre=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-'){
			f=-1;
		}
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		lre=(lre<<3)+(lre<<1)+(c-'0');
		c=getchar();
	}
	return lre*f;
}

struct edge{
	int head[maxn];
	int to[2*maxn],next[2*maxn],val[2*maxn];
	int e_cnt=1;
	void add(int u,int v,int w){
		to[e_cnt]=v;
		val[e_cnt]=w;
		next[e_cnt]=head[u];
		head[u]=e_cnt++;
	}
}E;

int n,m,clo=0;
int deg[maxn],tim[maxn];
//存在点度数不超过5
vector<pii> In[maxn];//tim从小到大
vector<pii> Out[maxn];//tim从大到小
queue<int> que; 

int q;
pii qry[maxn];

set<pii> st,st2;//k=2任意子问题,长短八 
map<pii,int> mp,mp2;

inline void add(int &x,int y){
	x=min(x,y);
}

signed main(){
	
//	freopen("1.in","r",stdin);
	
	n=read();m=read();
	for(int i=1;i<=m;i++){
		int u=read(),v=read(),w=read();
		E.add(u,v,w);E.add(v,u,w);
		deg[u]++;deg[v]++;
	}
	for(int i=1;i<=n;i++){
		if(deg[i]<=5){
			que.push(i);
			tim[i]=++clo;
		} 
	}
	while(!que.empty()){
		int x=que.front();que.pop();
		for(int i=E.head[x];i;i=E.next[i]){
			int v=E.to[i];
			deg[v]--;
			if(tim[v]==0&&deg[v]<=5){
				tim[v]=++clo;
				que.push(v);
			}
		}
	}//删点,每一个点被删前邻域内不超过5个点
	//等价于给无向图定向,使每一个点的后继结点不超过5个 
	for(int u=1;u<=n;u++){
		for(int i=E.head[u];i;i=E.next[i]){
			int v=E.to[i],w=E.val[i];
			if(tim[u]<tim[v]){
				//出边(不超过5条)
				Out[u].push_back({v,w}); 
			//	printf(" %d %d %d\n",u,v,w);
			}else{
				//入边(好像没什么卯用)
				In[u].push_back({v,w});
			}
		}
	}
//	putchar('\n');
	
	q=read();
	for(int i=1;i<=q;i++){
		int& s=qry[i].first=read();int& t=qry[i].second=read();
		if(tim[s]>tim[t])swap(s,t);
		for(pii p:Out[s]){
			st.insert({p.first,t});
		}
		for(pii p:Out[t]){
			st.insert({s,p.first});
		}
		st.insert({s,t});//直接自由两点 
		st2.insert({s,t});st2.insert({t,s});//长短八 
	}
	
	for(pii p:st)mp[p]=INF;
	for(pii p:st2)mp2[p]=INF;
	
	for(pii p:st){
		//内八问题
		int s=p.first,t=p.second;
		for(pii p1:Out[s]){
			for(pii p2:Out[t]){
				if(p1.first==p2.first){
					add(mp[p],p1.second+p2.second);
				}
			}
		} 
	} 
	
	for(int x=1;x<=n;x++){
		//链上问题
		for(pii p1:Out[x]){
			int y=p1.first,w1=p1.second;
			for(pii p2:Out[y]){
				int z=p2.first,w2=p2.second;
				if(st.count({x,z})){
					add(mp[{x,z}],w1+w2);
				}
				if(st.count({z,x})){
					add(mp[{z,x}],w1+w2);
				}
			}
		} 
	}
	
	for(int a=1;a<=n;a++){
		//外八 
		for(pii p1:In[a]){
			int b=p1.first,w1=p1.second;
			for(pii p2:Out[b]){
				int c=p2.first,w2=p2.second;
				if(b==c)continue;
				if(st.count({a,c}))add(mp[{a,c}],w1+w2);
				if(st.count({c,a}))add(mp[{c,a}],w1+w2);
				for(pii p3:Out[c]){
					int d=p3.first,w3=p3.second;
					if(st2.count({a,d}))add(mp2[{a,d}],w1+w2+w3);
				}
			}
		}
	}
	
	int s,t,ans;
	for(register int i=1;i<=q;++i){
		s=qry[i].first,t=qry[i].second; 
		ans=INF;
		for(pii p:Out[s]){
			add(ans,p.second+mp[{p.first,t}]);
			if(p.first==t)add(ans,p.second);
		}
		for(pii p:Out[t]){
			add(ans,p.second+mp[{s,p.first}]);
		}
		add(ans,mp[{s,t}]);add(ans,mp2[{s,t}]);add(ans,mp2[{t,s}]);
		if(ans==INF)ans=-1;
		printf("%d\n",ans);
	}
	
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 63308kb

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: 7ms
memory: 63180kb

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: 0
Accepted
time: 7710ms
memory: 195708kb

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:

ok 1000000 numbers

Test #4:

score: 0
Accepted
time: 8900ms
memory: 217056kb

input:

35479 70156
1 2 53094201
1 3 95796673
1 4 35585979
1 5 55612594
2 3 60766083
3 4 64392832
4 5 32896460
5 2 91649893
6 2 6196154
6 7 4986564
7 3 91799790
7 8 10909791
8 4 30034265
8 9 95672010
9 4 67004237
9 10 77872672
10 5 68900058
10 6 42927604
11 6 71288663
11 12 51597962
12 7 79690815
12 13 9742...

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:

ok 1000000 numbers

Test #5:

score: 0
Accepted
time: 8968ms
memory: 221020kb

input:

35811 70820
1 2 40434193
1 3 13483892
1 4 32864259
1 5 47591755
1 6 65123023
1 7 81695948
1 8 1102880
1 9 47223939
1 10 52947058
1 11 31439481
2 3 94162364
3 4 20590842
4 5 24137043
5 6 74926235
6 7 9376267
7 8 97130364
8 9 75568799
9 10 5022411
10 11 59066963
11 2 96177033
12 2 17823959
12 13 83906...

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:

ok 1000000 numbers

Test #6:

score: -100
Time Limit Exceeded

input:

200000 599952
127401 69434 88680591
127401 39916 10673559
127401 52475 59546013
127401 77787 74018113
127401 11462 7023970
60723 37187 65141305
60723 115008 72307785
60723 71812 47362248
60723 143858 20042617
60723 153890 48502784
60723 172009 21754689
60723 23327 97998405
63817 58332 30056889
63817...

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: