Submission #47722

# Submission time Handle Problem Language Result Execution time Memory
47722 2018-05-06T18:03:40 Z dqhungdl Evacuation plan (IZhO18_plan) C++17
54 / 100
4000 ms 43608 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX=17;
typedef pair<int,int> ii;
int n,m,k,T,d[100005],R[100005],h[100005],P[100005][20],mind[100005][20];
vector<ii> Edge,g[100005],gg[100005];
set<ii> s;

void ReadInt(int &number)
{
    number=0;
    int c=getchar();    
    while(c<'0'||c>'9')
    	c=getchar();
    for(;('0'<=c&&c<='9');c=getchar())
        number=number*10+c-48;
}

bool cmp(ii x1,ii x2)
{
	return min(d[x1.first],d[x1.second])>min(d[x2.first],d[x2.second]);
}

int Find(int u)
{
	if(u==R[u])
		return u;
	return R[u]=Find(R[u]);
}

void Union(int u,int v)
{
	if(u>v)
		R[u]=v;
	else
		R[v]=u;
}

void Enter()
{
	ReadInt(n);
	ReadInt(m);
	int u,v,w;
	while(m--)
	{
		ReadInt(u);
		ReadInt(v);
		ReadInt(w);
		Edge.push_back(ii(u,v));
		g[u].push_back(ii(v,w));
		g[v].push_back(ii(u,w));
	}
	for(int i=1;i<=n;i++)
	{
		d[i]=1e9;
		R[i]=i;
	}
	ReadInt(k);
	while(k--)
	{
		ReadInt(u);
		s.insert(ii(0,u));
		d[u]=0;
	}
}

void BFS()
{
	while(s.size()>0)
	{
		int u=(*s.begin()).second;
		s.erase(s.begin());
		for(int i=0;i<g[u].size();i++)
		{
			int v=g[u][i].first;
			int w=g[u][i].second;
			if(d[v]>d[u]+w)
			{
				if(d[v]!=1e9)
					s.erase(s.find(ii(d[v],v)));
				d[v]=d[u]+w;
				s.insert(ii(d[v],v));
			}
		}
	}
}

void DFS(int u,int high)
{
	h[u]=high;
	for(int i=0;i<gg[u].size();i++)
	{
		int v=gg[u][i].first;
		int w=gg[u][i].second;
		if(h[v]==0)
		{
			P[v][0]=u;
			mind[v][0]=w;
			DFS(v,high+1);					
		}
	}
}

int LCA(int u,int v)
{
	int rs=1e9;
    for(int k=MAX;k>=0;k--)
        if(h[P[u][k]]>=h[v])
        {
        	rs=min(rs,mind[u][k]);
            u=P[u][k];         
        }
        else
        if(h[P[v][k]]>=h[u])
        {
        	rs=min(rs,mind[v][k]);
            v=P[v][k];
        }
    for(int k=10;k>=0;k--)
        if(P[u][k]!=P[v][k])
        {
        	rs=min(rs,mind[u][k]);
        	rs=min(rs,mind[v][k]);
            u=P[u][k];
            v=P[v][k];
        }
    while(u!=v)
    {
    	rs=min(rs,mind[u][0]);
        rs=min(rs,mind[v][0]);
        u=P[u][0];
        v=P[v][0];
    }
    return rs;
}

void InitTree()
{
	sort(Edge.begin(),Edge.end(),cmp);
	for(int i=0;i<Edge.size();i++)
	{
		int u=Edge[i].first;
		int v=Edge[i].second;
		int w=min(d[u],d[v]);
		if(Find(u)!=Find(v))
		{
			Union(Find(u),Find(v));
			gg[u].push_back(ii(v,w));
			gg[v].push_back(ii(u,w));
		}
	}
	DFS(1,1);
	for(int k=1;k<=MAX;k++)
    	for(int i=1;i<=n;i++)
    	{
			P[i][k]=P[P[i][k-1]][k-1];
			mind[i][k]=min(mind[i][k-1],mind[P[i][k-1]][k-1]);
    	}
}

void Query()
{
	ReadInt(T);
	int u,v;
	while(T--)
	{
		ReadInt(u);
		ReadInt(v);
		cout<<LCA(u,v)<<"\n";
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	//freopen("TEST.INP","r",stdin);
	//freopen("TEST.OUT","w",stdout);
	Enter();
	BFS();
	InitTree();
	Query();
}

Compilation message

plan.cpp: In function 'void BFS()':
plan.cpp:74:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<g[u].size();i++)
               ~^~~~~~~~~~~~
plan.cpp: In function 'void DFS(int, int)':
plan.cpp:92:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<gg[u].size();i++)
              ~^~~~~~~~~~~~~
plan.cpp: In function 'void InitTree()':
plan.cpp:141:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<Edge.size();i++)
              ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 7 ms 5368 KB Output is correct
3 Correct 7 ms 5368 KB Output is correct
4 Correct 6 ms 4988 KB Output is correct
5 Correct 7 ms 5368 KB Output is correct
6 Correct 7 ms 5368 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 8 ms 5368 KB Output is correct
10 Correct 7 ms 5368 KB Output is correct
11 Correct 7 ms 5368 KB Output is correct
12 Correct 7 ms 5368 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 7 ms 5368 KB Output is correct
15 Correct 7 ms 5384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 7 ms 5368 KB Output is correct
3 Correct 7 ms 5368 KB Output is correct
4 Correct 6 ms 4988 KB Output is correct
5 Correct 7 ms 5368 KB Output is correct
6 Correct 7 ms 5368 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 8 ms 5368 KB Output is correct
10 Correct 7 ms 5368 KB Output is correct
11 Correct 7 ms 5368 KB Output is correct
12 Correct 7 ms 5368 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 7 ms 5368 KB Output is correct
15 Correct 7 ms 5384 KB Output is correct
16 Correct 232 ms 25620 KB Output is correct
17 Correct 785 ms 42756 KB Output is correct
18 Correct 50 ms 9200 KB Output is correct
19 Correct 232 ms 31912 KB Output is correct
20 Correct 755 ms 42612 KB Output is correct
21 Correct 398 ms 33452 KB Output is correct
22 Correct 222 ms 34412 KB Output is correct
23 Correct 766 ms 42756 KB Output is correct
24 Correct 775 ms 43188 KB Output is correct
25 Correct 785 ms 43432 KB Output is correct
26 Correct 227 ms 32096 KB Output is correct
27 Correct 220 ms 32132 KB Output is correct
28 Correct 234 ms 31964 KB Output is correct
29 Correct 225 ms 31836 KB Output is correct
30 Correct 221 ms 32348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5116 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 5112 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 7 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 343 ms 33144 KB Output is correct
2 Correct 684 ms 42412 KB Output is correct
3 Correct 688 ms 42316 KB Output is correct
4 Correct 185 ms 31824 KB Output is correct
5 Correct 684 ms 42224 KB Output is correct
6 Correct 702 ms 42368 KB Output is correct
7 Correct 690 ms 42480 KB Output is correct
8 Correct 670 ms 42764 KB Output is correct
9 Correct 690 ms 42844 KB Output is correct
10 Correct 682 ms 42656 KB Output is correct
11 Correct 715 ms 43076 KB Output is correct
12 Correct 697 ms 42932 KB Output is correct
13 Correct 697 ms 42968 KB Output is correct
14 Correct 699 ms 42984 KB Output is correct
15 Correct 708 ms 42988 KB Output is correct
16 Correct 712 ms 42768 KB Output is correct
17 Correct 699 ms 42732 KB Output is correct
18 Correct 730 ms 42796 KB Output is correct
19 Correct 171 ms 33384 KB Output is correct
20 Correct 746 ms 43032 KB Output is correct
21 Correct 595 ms 43608 KB Output is correct
22 Correct 178 ms 31620 KB Output is correct
23 Correct 173 ms 30656 KB Output is correct
24 Correct 185 ms 31456 KB Output is correct
25 Correct 185 ms 31556 KB Output is correct
26 Correct 191 ms 31180 KB Output is correct
27 Correct 186 ms 33488 KB Output is correct
28 Correct 172 ms 31420 KB Output is correct
29 Correct 189 ms 32772 KB Output is correct
30 Correct 174 ms 31684 KB Output is correct
31 Correct 186 ms 32724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 7 ms 5368 KB Output is correct
3 Correct 7 ms 5368 KB Output is correct
4 Correct 6 ms 4988 KB Output is correct
5 Correct 7 ms 5368 KB Output is correct
6 Correct 7 ms 5368 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 8 ms 5368 KB Output is correct
10 Correct 7 ms 5368 KB Output is correct
11 Correct 7 ms 5368 KB Output is correct
12 Correct 7 ms 5368 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 7 ms 5368 KB Output is correct
15 Correct 7 ms 5384 KB Output is correct
16 Correct 232 ms 25620 KB Output is correct
17 Correct 785 ms 42756 KB Output is correct
18 Correct 50 ms 9200 KB Output is correct
19 Correct 232 ms 31912 KB Output is correct
20 Correct 755 ms 42612 KB Output is correct
21 Correct 398 ms 33452 KB Output is correct
22 Correct 222 ms 34412 KB Output is correct
23 Correct 766 ms 42756 KB Output is correct
24 Correct 775 ms 43188 KB Output is correct
25 Correct 785 ms 43432 KB Output is correct
26 Correct 227 ms 32096 KB Output is correct
27 Correct 220 ms 32132 KB Output is correct
28 Correct 234 ms 31964 KB Output is correct
29 Correct 225 ms 31836 KB Output is correct
30 Correct 221 ms 32348 KB Output is correct
31 Correct 6 ms 5116 KB Output is correct
32 Correct 6 ms 5112 KB Output is correct
33 Correct 6 ms 5112 KB Output is correct
34 Correct 6 ms 5112 KB Output is correct
35 Correct 6 ms 5112 KB Output is correct
36 Correct 6 ms 5112 KB Output is correct
37 Correct 6 ms 5112 KB Output is correct
38 Correct 6 ms 5112 KB Output is correct
39 Correct 6 ms 5112 KB Output is correct
40 Correct 7 ms 5112 KB Output is correct
41 Correct 343 ms 33144 KB Output is correct
42 Correct 684 ms 42412 KB Output is correct
43 Correct 688 ms 42316 KB Output is correct
44 Correct 185 ms 31824 KB Output is correct
45 Correct 684 ms 42224 KB Output is correct
46 Correct 702 ms 42368 KB Output is correct
47 Correct 690 ms 42480 KB Output is correct
48 Correct 670 ms 42764 KB Output is correct
49 Correct 690 ms 42844 KB Output is correct
50 Correct 682 ms 42656 KB Output is correct
51 Correct 715 ms 43076 KB Output is correct
52 Correct 697 ms 42932 KB Output is correct
53 Correct 697 ms 42968 KB Output is correct
54 Correct 699 ms 42984 KB Output is correct
55 Correct 708 ms 42988 KB Output is correct
56 Correct 712 ms 42768 KB Output is correct
57 Correct 699 ms 42732 KB Output is correct
58 Correct 730 ms 42796 KB Output is correct
59 Correct 171 ms 33384 KB Output is correct
60 Correct 746 ms 43032 KB Output is correct
61 Correct 595 ms 43608 KB Output is correct
62 Correct 178 ms 31620 KB Output is correct
63 Correct 173 ms 30656 KB Output is correct
64 Correct 185 ms 31456 KB Output is correct
65 Correct 185 ms 31556 KB Output is correct
66 Correct 191 ms 31180 KB Output is correct
67 Correct 186 ms 33488 KB Output is correct
68 Correct 172 ms 31420 KB Output is correct
69 Correct 189 ms 32772 KB Output is correct
70 Correct 174 ms 31684 KB Output is correct
71 Correct 186 ms 32724 KB Output is correct
72 Correct 419 ms 34040 KB Output is correct
73 Correct 745 ms 43444 KB Output is correct
74 Correct 768 ms 43472 KB Output is correct
75 Correct 814 ms 43400 KB Output is correct
76 Correct 821 ms 43344 KB Output is correct
77 Correct 761 ms 43324 KB Output is correct
78 Correct 768 ms 43484 KB Output is correct
79 Correct 773 ms 43200 KB Output is correct
80 Correct 760 ms 43072 KB Output is correct
81 Correct 788 ms 43152 KB Output is correct
82 Correct 780 ms 42564 KB Output is correct
83 Correct 776 ms 42828 KB Output is correct
84 Correct 817 ms 43036 KB Output is correct
85 Correct 788 ms 43424 KB Output is correct
86 Correct 757 ms 43324 KB Output is correct
87 Correct 768 ms 42828 KB Output is correct
88 Correct 755 ms 43576 KB Output is correct
89 Execution timed out 4093 ms 33320 KB Time limit exceeded
90 Halted 0 ms 0 KB -