QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#290156#5034. >.<The_Last_Candy20 531ms189444kbC++144.0kb2023-12-24 14:50:362023-12-24 14:50:36

Judging History

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

  • [2023-12-24 14:50:36]
  • 评测
  • 测评结果:20
  • 用时:531ms
  • 内存:189444kb
  • [2023-12-24 14:50:36]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct ACAM{
	map <int, int> son,w;
	int fail,siz;
}t[N * 3];
int tot = 0,pt[N],n,m,k,eu[N],ev[N],ew[N],seq[N],len = 0,ban[N * 3];
map <pair <int,int> , int> path;
struct Segment_Tree{
	int cnt = 0,root[N * 3],lc[N * 30],rc[N * 30],siz[N * 30],w[N * 30],num[N * 30];
	inline void pushup(int pos) {siz[pos] = siz[lc[pos]] + siz[rc[pos]];}
	inline void copy(int x,int y)
	{
		lc[x] = lc[y]; rc[x] = rc[y]; siz[x] = siz[y]; w[x] = w[y]; 
	}
	inline int modify(int l,int r,int x,int k,int p,int pos)
	{
		int cur = ++cnt;
		copy(cur,pos);
		if(l == r) {siz[cur] = 1; w[cur] = k; num[cur] = p; return cur;}
		int mid = (l + r) >> 1;
		if(x <= mid) lc[cur] = modify(l,mid,x,k,p,lc[pos]);
		else rc[cur] = modify(mid + 1,r,x,k,p,rc[pos]);
		pushup(cur);
		return cur;
	}
	inline int query_num(int l,int r,int x,int pos)
	{
		if(!pos) return 0;
		if(l == r) return num[pos];
		int mid = (l + r) >> 1,ret = 0;
		if(x <= mid) ret = query_num(l,mid,x,lc[pos]);
		else ret = query_num(mid + 1,r,x,rc[pos]);
		return ret;
	} 
	inline pair <int,int> findmini(int l,int r,int pos)
	{
		if(l == r) {siz[pos]--; return make_pair(num[pos],w[pos]);}
		int mid = (l + r) >> 1; pair <int,int> ret = make_pair(0,0);
		if(siz[lc[pos]] > 0) ret = findmini(l,mid,lc[pos]);
		else ret = findmini(mid + 1,r,rc[pos]);
		pushup(pos);
		return ret; 
	} 
}tr;
inline void build()
{
	static int vis[N];
	memset(vis,0,sizeof(vis));
	queue <int> q;
	for(int i = 1;i <= n;i++) 
		if(t[0].son.find(i) != t[0].son.end())
		{
			tr.root[0] = tr.modify(1,n,i,0,t[0].son[i],tr.root[0]);
			q.push(t[0].son[i]),t[t[0].son[i]].fail = 0;
		}
	while(!q.empty())
	{
		int x = q.front(); q.pop();
		if(vis[x]) continue;
		vis[x] = 1;
		if(t[x].fail != 0)
			tr.copy(tr.root[x],tr.root[t[x].fail]);
		for(map <int,int> :: iterator it = t[x].son.begin(),id = t[x].w.begin();it != t[x].son.end();it++,id++) 
		{
			tr.root[x] = tr.modify(1,n,(*it).first,(*id).second,(*it).second,tr.root[x]);
			t[(*it).second].fail = tr.query_num(1,n,(*it).first,tr.root[t[x].fail]);
			q.push((*it).second);
		}
	}
}
typedef long long ll;
inline ll dijkstra()
{
	static int vis[N];
	static ll dis[N];
	ll ret = 0x3f3f3f3f3f3f3f3f;
	memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis));
	dis[1] = 0;
	priority_queue < pair<int,int> , vector< pair<int,int> > , greater< pair<int,int> > > q;
	q.push(make_pair(0,1));
	while(!q.empty())
	{
		int x = q.top().second; q.pop();
		if(vis[x] || ban[x] == 1) continue;
		if(ban[x] == 2) ret = min(ret,dis[x]);
		while(tr.siz[tr.root[x]])
		{
			pair <int,int> now = tr.findmini(1,n,tr.root[x]);
			if(dis[now.first] > dis[x] + now.second)
			{
				dis[now.first] = dis[x] + now.second;
				q.push(make_pair(dis[now.first],now.first));
			}
		}
	}
	return (ret == 0x3f3f3f3f3f3f3f3f ? -1 : ret);
}
int main()
{
	cin>>n>>m>>k;
	for(int i = 1;i <= m;i++)
		cin>>eu[i]>>ev[i]>>ew[i],path[make_pair(eu[i],ev[i])] = ew[i];
	for(int i = 1;i <= k;i++)
	{
		cin>>len;
		for(int j = 1;j <= len;j++) cin>>seq[j];
		int flag = 1;
		for(int j = 1;j <= len - 1;j++)
			if(path.find(make_pair(seq[j],seq[j + 1])) == path.end())
				flag = 0;
		if(!flag) continue;
		int np = 0;
		for(int j = 1;j <= len;j++)
		{
			if(t[np].son.find(seq[j]) == t[np].son.end())
			{
				t[np].son[seq[j]] = ++tot;
				if(j > 1) t[np].w[seq[j]] = path[make_pair(seq[j - 1],seq[j])];
				else t[np].w[seq[j]] = 0;
			}
			np = t[np].son[seq[j]];
			if(seq[j] == n) ban[np] = 2;
		}
		ban[np] = 1;
	}
	for(int i = 1;i <= n;i++) 
	{
		if(t[0].son.find(i) == t[0].son.end())
			t[0].son[i] = ++tot,t[0].w[i] = 0;
		pt[i] = t[0].son[i];
	}
	ban[pt[n]] = 2;
	for(int i = 1,np;i <= m;i++)
	{
		np = t[0].son[eu[i]];
		if(t[np].son.find(ev[i]) != t[np].son.end()) continue;
		t[np].son[ev[i]] = pt[ev[i]];
		t[np].w[ev[i]] = ew[i];
	}
	for(int i = 1;i <= tot;i++) if(!tr.root[i]) tr.root[i] = ++tr.cnt;
	build();
	cout<<dijkstra();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 20
Accepted

Test #1:

score: 20
Accepted
time: 8ms
memory: 69200kb

input:

35 100 0
34 7 447733879
24 20 187005344
14 34 654502303
2 31 865194349
20 33 9517055
33 15 991889891
24 33 395599993
13 16 237525328
9 5 373850826
30 34 391470240
10 7 650077565
26 10 400825980
34 27 189924713
19 27 907609573
20 10 614945312
10 5 960007605
1 7 984076202
32 25 539699728
24 31 2553027...

output:

1970522617

result:

ok single line: '1970522617'

Test #2:

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

input:

35 100 0
3 12 720466531
8 12 396056603
29 21 717362482
34 13 785882968
7 13 748993858
9 28 371041056
5 22 279747660
10 13 511029466
9 10 90421686
24 13 68485905
12 17 589986641
26 3 49095373
15 24 515201376
10 33 672973479
29 31 705185599
27 22 689337965
20 4 145960570
31 28 136121037
28 5 202143094...

output:

2296067497

result:

ok single line: '2296067497'

Test #3:

score: 0
Accepted
time: 7ms
memory: 67956kb

input:

35 100 0
22 20 355360466
23 35 601550723
3 27 186544474
6 24 134507727
25 2 672165808
19 1 711018563
32 16 669385420
27 11 750652665
14 11 158441860
25 14 53347528
2 20 140122295
33 20 112964489
14 6 253781013
18 14 771123144
17 35 508607402
3 19 403442205
30 16 336645858
24 22 470183063
31 22 10734...

output:

1517028140

result:

ok single line: '1517028140'

Test #4:

score: 0
Accepted
time: 11ms
memory: 68736kb

input:

35 100 0
32 5 808438527
26 23 888346324
14 19 992303007
23 1 278329540
17 29 587913784
4 33 770924125
2 5 605204525
1 21 657667587
9 35 444108546
22 12 391857443
31 33 184589665
7 14 826884170
10 32 241928783
3 17 634515992
9 34 429624654
1 18 736971857
6 9 625772037
20 18 344038507
12 25 24408330
4...

output:

1502429426

result:

ok single line: '1502429426'

Test #5:

score: 0
Accepted
time: 18ms
memory: 69012kb

input:

35 100 0
1 9 150223804
13 25 225623874
27 10 826064515
7 31 111586392
27 4 627187519
8 7 517189480
10 13 427167940
24 14 563496
27 23 119441879
13 31 712972744
34 13 128158051
16 13 146964967
31 14 860155206
25 5 431208773
24 11 48709486
29 10 694088474
11 1 801122521
12 10 369399315
21 29 399505482...

output:

1355451140

result:

ok single line: '1355451140'

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #6:

score: 0
Wrong Answer
time: 15ms
memory: 68236kb

input:

35 100 10
11 2 380526516
9 1 213280408
20 1 775174358
23 33 14349082
32 11 781584201
10 26 572662203
8 12 157664649
23 20 327195474
15 25 861545590
6 18 838910534
21 27 91640650
19 26 995166014
4 2 878565098
4 34 523383573
26 18 578962566
31 6 874478934
11 8 398592349
10 7 643306798
13 28 290421417
...

output:

3325528088

result:

wrong answer 1st lines differ - expected: '1980354133', found: '3325528088'

Subtask #3:

score: 0
Wrong Answer

Test #11:

score: 0
Wrong Answer
time: 531ms
memory: 189444kb

input:

50000 200000 1
7542 37166 116613326
3581 43961 629220971
12873 42953 440313807
31744 5286 697832848
25882 12748 106667302
34760 29676 181570340
41570 9240 885513989
22227 35688 63657981
43180 29194 174058940
8977 41899 48262624
7465 18291 600002514
46925 9281 951474878
2115 31162 373758881
5386 3798...

output:

4318565547

result:

wrong answer 1st lines differ - expected: '2526392504', found: '4318565547'

Subtask #4:

score: 0
Skipped

Dependency #2:

0%