답안 #46948

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
46948 2018-04-25T10:48:18 Z zscoder Wild Boar (JOI18_wild_boar) C++17
62 / 100
18000 ms 741424 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<ll>::iterator sit;
typedef map<ll,ll>::iterator mit;
 
vector<pair<int,int> > edges;
ll dist[4111][4111];
int n,m,q,L; 
vector<pair<ii,int> > adj[2111];
vi in[2111];
vi out[2111];
ll orid[2001][2001];
int getid(int a, int b)
{
	return (a*2+b);
}
const ll INF=ll(1e18);
int a[211111];
ll dp[2][5];
ll D[2111][2111][5];
vector<pair<int,int> > paths[2111][2111];
vector<ll> W;

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin>>n>>m>>q>>L;
	for(int i=0;i<m;i++)
	{
		int u,v,w; cin>>u>>v>>w; u--; v--; edges.pb({u,v}); W.pb(w); edges.pb({v,u}); W.pb(w); orid[u][v]=orid[v][u]=w;
		adj[u].pb(mp(mp(v,w),i*2)); adj[v].pb(mp(mp(u,w),i*2+1));
		out[u].pb(i*2); out[v].pb(i*2+1);
		in[u].pb(i*2+1); in[v].pb(i*2);
	}
	/*
	int orim = m;
	for(int i=0;i<n;i++)
	{
		adj[n].pb(mp(mp(i,0),m++)); edges.pb({n,i}); W.pb(0);
	}
	*/
	int orim = m; m*=2;
	for(int i=0;i<m;i++)
	{
		priority_queue<ii,vector<ii>,greater<ii> > pq;
		int s = i;
		pq.push(mp(0,s));
		for(int k=0;k<m;k++) dist[s][k] = INF;
		dist[s][s] = 0;
		while(!pq.empty())
		{
			ll d=pq.top().fi; int u = pq.top().se; pq.pop();
			//cerr<<"DIST : "<<s<<' '<<u<<' '<<d<<'\n';
			int vertex = edges[u].se; int edgeno = u;
			for(auto k:adj[vertex])
			{
				int v = k.fi.fi; ll w = k.fi.se; int id = k.se;
				if(id/2==edgeno/2) continue;
				int u1 = edges[id].fi; int v1 = edges[id].se;
				assert(u1==vertex);
				//if(edges[id][0]!=v) vid++;
				if(dist[s][id]>d)
				{
					dist[s][id] = d;
					pq.push(mp(d+w, id));
				}
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(i==j) continue;
			ll mn=INF; ii cur=mp(-1,-1);
			for(int k:out[i])
			{
				for(int l:in[j])
				{
					if(dist[k][l]+W[k]+W[l]<mn)
					{
						mn=dist[k][l]+W[k]+W[l];
						cur=mp(edges[k].se, edges[l].fi);
					}
				}
			}
			if(orid[i][j]>0)
			{
				if(orid[i][j]<mn)
				{
					mn=orid[i][j];
					cur=mp(j,i);
				}
			}
			D[i][j][0] = mn; //D[j][i][0] = mn;
			paths[i][j].pb(cur); //paths[j][i].pb(cur);
			mn = INF; cur = mp(-1,-1);
			for(int k:out[i])
			{
				for(int l:in[j])
				{
					//cerr<<"OUT "<<i<<' '<<j<<' '<<k<<' '<<l<<'\n';
					if(edges[k].se!=paths[i][j][0].fi&&dist[k][l]+W[k]+W[l]<mn)
					{
						//cerr<<"OUT 2 : "<<i<<' '<<j<<' '<<k<<' '<<l<<'\n';
						mn=dist[k][l]+W[k]+W[l];
						cur=mp(edges[k].se, edges[l].fi);
					}
				}
			}
			if(orid[i][j]>0)
			{
				if(orid[i][j]<mn&&j!=paths[i][j][0].fi)
				{
					mn=orid[i][j];
					cur=mp(j,i);
				}
			}
			D[i][j][1] = mn; //= D[j][i][1] = mn;
			paths[i][j].pb(cur); //paths[j][i].pb(cur);
			mn = INF; cur = mp(-1,-1);
			for(int k:out[i])
			{
				for(int l:in[j])
				{
					if(edges[k].se!=paths[i][j][0].fi&&edges[l].fi!=paths[i][j][1].se&&dist[k][l]+W[k]+W[l]<mn)
					{
						mn=dist[k][l]+W[k]+W[l];
						cur=mp(edges[k].se, edges[l].fi);
					}
				}
			}
			if(orid[i][j]>0)
			{
				if(orid[i][j]<mn&&j!=paths[i][j][0].fi&&i!=paths[i][j][1].se)
				{
					mn=orid[i][j];
					cur=mp(j,i);
				}
			}
			D[i][j][2] = mn; //D[j][i][2] = mn;
			paths[i][j].pb(cur); //paths[j][i].pb(cur);
			mn = INF; cur = mp(-1,-1);
			for(int k:out[i])
			{
				for(int l:in[j])
				{
					if(edges[l].fi!=paths[i][j][0].se&&dist[k][l]+W[k]+W[l]<mn)
					{
						mn=dist[k][l]+W[k]+W[l];
						cur=mp(edges[k].se, edges[l].fi);
					}
				}
			}
			if(orid[i][j]>0)
			{
				if(orid[i][j]<mn&&i!=paths[i][j][0].se)
				{
					mn=orid[i][j];
					cur=mp(j,i);
				}
			}
			D[i][j][3] = mn; // D[j][i][3] = mn;
			paths[i][j].pb(cur); //paths[j][i].pb(cur);
			mn = INF; cur = mp(-1,-1);
			for(int k:out[i])
			{
				for(int l:in[j])
				{
					if(edges[k].se!=paths[i][j][3].fi&&edges[l].fi!=paths[i][j][0].se&&dist[k][l]+W[k]+W[l]<mn)
					{
						mn=dist[k][l]+W[k]+W[l];
						cur=mp(edges[k].se, edges[l].fi);
					}
				}
			}
			if(orid[i][j]>0)
			{
				if(orid[i][j]<mn&&j!=paths[i][j][3].fi&&i!=paths[i][j][0].se)
				{
					mn=orid[i][j];
					cur=mp(j,i);
				}
			}
			D[i][j][4] = mn; //D[j][i][2] = mn;
			paths[i][j].pb(cur); //paths[j][i].pb(cur);
		}
	}
	for(int i=0;i<L;i++)
	{
		cin>>a[i]; a[i]--;
	}
	for(int zz=0;zz<q;zz++)
	{
		int y,z; cin>>y>>z; y--; z--; a[y]=z;
		/*
		for(int i=0;i<m*2;i++)
		{
			dp[0][i] = INF;
		}
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<2;j++)
			{
				if(edges[i][j]==a[0])
				{
					dp[0][getid(i,j)] = 0;
				}
			}
		}
		*/
		/*
		dp[0][getid(orim + a[0],1)] = 0;
		for(int i=0;i+1<L;i++)
		{
			int pre = i&1; int cur = pre^1;
			for(int j=0;j<m*2;j++)
			{
				dp[cur][j] = INF;
			}
			for(int j=0;j<m*2;j++)
			{
				if(dp[pre][j]<INF)
				{
					ll v=dp[pre][j];
					for(int k:belong[a[i+1]])
					{
						if(dist[j][k]<INF)
						{
							dp[cur][k] = min(dp[cur][k], v + dist[j][k]);
						}
					}
				}
			}
		}
		ll mn = INF;
		for(int i=0;i<m*2;i++)
		{
			mn = min(mn, dp[(L+1)&1][i]);
		}
		
		cout<<mn<<'\n';
		*/
		for(int i=0;i<5;i++) dp[1][i] = D[a[0]][a[1]][i];
		/*
		for(int j=0;j<5;j++)
		{
			cerr<<0<<' '<<j<<' '<<paths[a[0]][a[1]][j].fi<<' '<<paths[a[0]][a[1]][j].se<<' '<<dp[1][j]<<'\n';
		}
		*/
		for(int i=1;i+1<L;i++)
		{
			int pre = i&1; int cur = pre^1;
			for(int j=0;j<5;j++)
			{
				dp[cur][j]=INF;
			}
			for(int j=0;j<5;j++)
			{
				if(dp[pre][j]<INF)
				{
					ll v=dp[pre][j];
					for(int k=0;k<5;k++)
					{
						//cerr<<i<<' '<<j<<' '<<k<<' '<<paths[a[i-1]][a[i]][j].fi<<' '<<paths[a[i-1]][a[i]][j].se<<' '<<paths[a[i]][a[i+1]][k].fi<<' '<<paths[a[i]][a[i+1]][k].se<<' '<<D[a[i-1]][a[i]][j]<<' '<<D[a[i]][a[i+1]][k]<<'\n';
						if(paths[a[i-1]][a[i]][j].se!=-1&&paths[a[i]][a[i+1]][k].fi!=-1&&paths[a[i-1]][a[i]][j].se!=paths[a[i]][a[i+1]][k].fi)
						{
							dp[cur][k] = min(dp[cur][k], v + D[a[i]][a[i+1]][k]);
						}
					}
				}
			}
			/*
			for(int j=0;j<5;j++)
			{
				cerr<<i<<' '<<j<<' '<<paths[a[i]][a[i+1]][j].fi<<' '<<paths[a[i]][a[i+1]][j].se<<' '<<dp[cur][j]<<'\n';
			}
			*/
		}
		ll mn = INF;
		for(int i=0;i<5;i++) mn=min(mn,dp[(L+1)&1][i]);
		if(mn>=INF) mn=-1;
		cout<<mn<<'\n';
	}
}

Compilation message

wild_boar.cpp: In function 'int main()':
wild_boar.cpp:74:9: warning: unused variable 'v' [-Wunused-variable]
     int v = k.fi.fi; ll w = k.fi.se; int id = k.se;
         ^
wild_boar.cpp:76:32: warning: unused variable 'v1' [-Wunused-variable]
     int u1 = edges[id].fi; int v1 = edges[id].se;
                                ^~
wild_boar.cpp:59:6: warning: unused variable 'orim' [-Wunused-variable]
  int orim = m; m*=2;
      ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 105172 KB Output is correct
2 Correct 93 ms 105576 KB Output is correct
3 Correct 87 ms 105576 KB Output is correct
4 Correct 85 ms 105576 KB Output is correct
5 Correct 86 ms 105576 KB Output is correct
6 Correct 87 ms 105576 KB Output is correct
7 Correct 87 ms 105576 KB Output is correct
8 Correct 95 ms 105676 KB Output is correct
9 Correct 101 ms 105676 KB Output is correct
10 Correct 87 ms 105696 KB Output is correct
11 Correct 86 ms 105696 KB Output is correct
12 Correct 85 ms 105708 KB Output is correct
13 Correct 85 ms 105708 KB Output is correct
14 Correct 90 ms 105848 KB Output is correct
15 Correct 87 ms 105956 KB Output is correct
16 Correct 86 ms 105956 KB Output is correct
17 Correct 86 ms 105956 KB Output is correct
18 Correct 89 ms 105956 KB Output is correct
19 Correct 86 ms 105956 KB Output is correct
20 Correct 85 ms 105956 KB Output is correct
21 Correct 87 ms 105956 KB Output is correct
22 Correct 91 ms 105956 KB Output is correct
23 Correct 86 ms 105956 KB Output is correct
24 Correct 86 ms 105956 KB Output is correct
25 Correct 90 ms 105956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 105172 KB Output is correct
2 Correct 93 ms 105576 KB Output is correct
3 Correct 87 ms 105576 KB Output is correct
4 Correct 85 ms 105576 KB Output is correct
5 Correct 86 ms 105576 KB Output is correct
6 Correct 87 ms 105576 KB Output is correct
7 Correct 87 ms 105576 KB Output is correct
8 Correct 95 ms 105676 KB Output is correct
9 Correct 101 ms 105676 KB Output is correct
10 Correct 87 ms 105696 KB Output is correct
11 Correct 86 ms 105696 KB Output is correct
12 Correct 85 ms 105708 KB Output is correct
13 Correct 85 ms 105708 KB Output is correct
14 Correct 90 ms 105848 KB Output is correct
15 Correct 87 ms 105956 KB Output is correct
16 Correct 86 ms 105956 KB Output is correct
17 Correct 86 ms 105956 KB Output is correct
18 Correct 89 ms 105956 KB Output is correct
19 Correct 86 ms 105956 KB Output is correct
20 Correct 85 ms 105956 KB Output is correct
21 Correct 87 ms 105956 KB Output is correct
22 Correct 91 ms 105956 KB Output is correct
23 Correct 86 ms 105956 KB Output is correct
24 Correct 86 ms 105956 KB Output is correct
25 Correct 90 ms 105956 KB Output is correct
26 Correct 90 ms 106172 KB Output is correct
27 Correct 100 ms 109376 KB Output is correct
28 Correct 102 ms 109808 KB Output is correct
29 Correct 201 ms 114284 KB Output is correct
30 Correct 180 ms 114488 KB Output is correct
31 Correct 181 ms 114684 KB Output is correct
32 Correct 205 ms 114896 KB Output is correct
33 Correct 215 ms 117320 KB Output is correct
34 Correct 190 ms 117480 KB Output is correct
35 Correct 177 ms 117592 KB Output is correct
36 Correct 178 ms 117920 KB Output is correct
37 Correct 191 ms 118120 KB Output is correct
38 Correct 196 ms 121140 KB Output is correct
39 Correct 176 ms 121284 KB Output is correct
40 Correct 203 ms 121556 KB Output is correct
41 Correct 204 ms 121944 KB Output is correct
42 Correct 172 ms 124036 KB Output is correct
43 Correct 192 ms 125648 KB Output is correct
44 Correct 193 ms 126080 KB Output is correct
45 Correct 198 ms 129980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 105172 KB Output is correct
2 Correct 93 ms 105576 KB Output is correct
3 Correct 87 ms 105576 KB Output is correct
4 Correct 85 ms 105576 KB Output is correct
5 Correct 86 ms 105576 KB Output is correct
6 Correct 87 ms 105576 KB Output is correct
7 Correct 87 ms 105576 KB Output is correct
8 Correct 95 ms 105676 KB Output is correct
9 Correct 101 ms 105676 KB Output is correct
10 Correct 87 ms 105696 KB Output is correct
11 Correct 86 ms 105696 KB Output is correct
12 Correct 85 ms 105708 KB Output is correct
13 Correct 85 ms 105708 KB Output is correct
14 Correct 90 ms 105848 KB Output is correct
15 Correct 87 ms 105956 KB Output is correct
16 Correct 86 ms 105956 KB Output is correct
17 Correct 86 ms 105956 KB Output is correct
18 Correct 89 ms 105956 KB Output is correct
19 Correct 86 ms 105956 KB Output is correct
20 Correct 85 ms 105956 KB Output is correct
21 Correct 87 ms 105956 KB Output is correct
22 Correct 91 ms 105956 KB Output is correct
23 Correct 86 ms 105956 KB Output is correct
24 Correct 86 ms 105956 KB Output is correct
25 Correct 90 ms 105956 KB Output is correct
26 Correct 90 ms 106172 KB Output is correct
27 Correct 100 ms 109376 KB Output is correct
28 Correct 102 ms 109808 KB Output is correct
29 Correct 201 ms 114284 KB Output is correct
30 Correct 180 ms 114488 KB Output is correct
31 Correct 181 ms 114684 KB Output is correct
32 Correct 205 ms 114896 KB Output is correct
33 Correct 215 ms 117320 KB Output is correct
34 Correct 190 ms 117480 KB Output is correct
35 Correct 177 ms 117592 KB Output is correct
36 Correct 178 ms 117920 KB Output is correct
37 Correct 191 ms 118120 KB Output is correct
38 Correct 196 ms 121140 KB Output is correct
39 Correct 176 ms 121284 KB Output is correct
40 Correct 203 ms 121556 KB Output is correct
41 Correct 204 ms 121944 KB Output is correct
42 Correct 172 ms 124036 KB Output is correct
43 Correct 192 ms 125648 KB Output is correct
44 Correct 193 ms 126080 KB Output is correct
45 Correct 198 ms 129980 KB Output is correct
46 Correct 355 ms 129980 KB Output is correct
47 Correct 7295 ms 275472 KB Output is correct
48 Correct 6305 ms 325308 KB Output is correct
49 Correct 5367 ms 370416 KB Output is correct
50 Correct 5734 ms 370796 KB Output is correct
51 Correct 6088 ms 371012 KB Output is correct
52 Correct 4184 ms 372060 KB Output is correct
53 Correct 4152 ms 372308 KB Output is correct
54 Correct 4255 ms 372820 KB Output is correct
55 Correct 4151 ms 373200 KB Output is correct
56 Correct 4142 ms 399456 KB Output is correct
57 Correct 4118 ms 428320 KB Output is correct
58 Correct 4204 ms 459012 KB Output is correct
59 Correct 4223 ms 492368 KB Output is correct
60 Correct 4180 ms 527820 KB Output is correct
61 Correct 4142 ms 565452 KB Output is correct
62 Correct 4066 ms 605504 KB Output is correct
63 Correct 3910 ms 647904 KB Output is correct
64 Correct 2337 ms 739004 KB Output is correct
65 Correct 2364 ms 739576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 105172 KB Output is correct
2 Correct 93 ms 105576 KB Output is correct
3 Correct 87 ms 105576 KB Output is correct
4 Correct 85 ms 105576 KB Output is correct
5 Correct 86 ms 105576 KB Output is correct
6 Correct 87 ms 105576 KB Output is correct
7 Correct 87 ms 105576 KB Output is correct
8 Correct 95 ms 105676 KB Output is correct
9 Correct 101 ms 105676 KB Output is correct
10 Correct 87 ms 105696 KB Output is correct
11 Correct 86 ms 105696 KB Output is correct
12 Correct 85 ms 105708 KB Output is correct
13 Correct 85 ms 105708 KB Output is correct
14 Correct 90 ms 105848 KB Output is correct
15 Correct 87 ms 105956 KB Output is correct
16 Correct 86 ms 105956 KB Output is correct
17 Correct 86 ms 105956 KB Output is correct
18 Correct 89 ms 105956 KB Output is correct
19 Correct 86 ms 105956 KB Output is correct
20 Correct 85 ms 105956 KB Output is correct
21 Correct 87 ms 105956 KB Output is correct
22 Correct 91 ms 105956 KB Output is correct
23 Correct 86 ms 105956 KB Output is correct
24 Correct 86 ms 105956 KB Output is correct
25 Correct 90 ms 105956 KB Output is correct
26 Correct 90 ms 106172 KB Output is correct
27 Correct 100 ms 109376 KB Output is correct
28 Correct 102 ms 109808 KB Output is correct
29 Correct 201 ms 114284 KB Output is correct
30 Correct 180 ms 114488 KB Output is correct
31 Correct 181 ms 114684 KB Output is correct
32 Correct 205 ms 114896 KB Output is correct
33 Correct 215 ms 117320 KB Output is correct
34 Correct 190 ms 117480 KB Output is correct
35 Correct 177 ms 117592 KB Output is correct
36 Correct 178 ms 117920 KB Output is correct
37 Correct 191 ms 118120 KB Output is correct
38 Correct 196 ms 121140 KB Output is correct
39 Correct 176 ms 121284 KB Output is correct
40 Correct 203 ms 121556 KB Output is correct
41 Correct 204 ms 121944 KB Output is correct
42 Correct 172 ms 124036 KB Output is correct
43 Correct 192 ms 125648 KB Output is correct
44 Correct 193 ms 126080 KB Output is correct
45 Correct 198 ms 129980 KB Output is correct
46 Correct 355 ms 129980 KB Output is correct
47 Correct 7295 ms 275472 KB Output is correct
48 Correct 6305 ms 325308 KB Output is correct
49 Correct 5367 ms 370416 KB Output is correct
50 Correct 5734 ms 370796 KB Output is correct
51 Correct 6088 ms 371012 KB Output is correct
52 Correct 4184 ms 372060 KB Output is correct
53 Correct 4152 ms 372308 KB Output is correct
54 Correct 4255 ms 372820 KB Output is correct
55 Correct 4151 ms 373200 KB Output is correct
56 Correct 4142 ms 399456 KB Output is correct
57 Correct 4118 ms 428320 KB Output is correct
58 Correct 4204 ms 459012 KB Output is correct
59 Correct 4223 ms 492368 KB Output is correct
60 Correct 4180 ms 527820 KB Output is correct
61 Correct 4142 ms 565452 KB Output is correct
62 Correct 4066 ms 605504 KB Output is correct
63 Correct 3910 ms 647904 KB Output is correct
64 Correct 2337 ms 739004 KB Output is correct
65 Correct 2364 ms 739576 KB Output is correct
66 Correct 1549 ms 739576 KB Output is correct
67 Correct 727 ms 739576 KB Output is correct
68 Correct 2872 ms 739576 KB Output is correct
69 Correct 2630 ms 740484 KB Output is correct
70 Execution timed out 18057 ms 741424 KB Time limit exceeded
71 Halted 0 ms 0 KB -