Submission #43534742


Source Code Expand

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
const ll MAXN = 410,INF = 1e18;
void tomin(ll& x,ll y){x=min(x,y);}

int n,m;
ll e[MAXN][MAXN],ans = INF;
int tag[MAXN][MAXN];

int ban[MAXN];
ll dis[MAXN],vis[MAXN],pre[MAXN];

int lab[MAXN];
vector<int>t[MAXN];

void dfs(int u){
	for(auto v:t[u])lab[v] = lab[u],dfs(v);
}

ll dij(int x){
	memset(tag,0,sizeof tag);
	rep(i,1,n)dis[i] = INF,vis[i] = pre[i] = 0;
	dis[x] = 0;
	while(1){
		int u = 0;
		rep(i,1,n)if(!vis[i] && (!u || dis[u] > dis[i]))u = i;
		if(!u)break;
		vis[u] = 1;
		rep(v,1,n)if(!ban[v] && dis[v] > dis[u] + e[u][v]){
			tag[pre[v]][v] = tag[v][pre[v]] = 0;

			dis[v] = dis[u] + e[u][v];
			pre[v] = u;

			tag[pre[v]][v] = tag[v][pre[v]] = 1;
		}
	}

	rep(i,1,n)t[i].clear();
	rep(i,1,n)if(pre[i])t[pre[i]].push_back(i);

	lab[x] = x;
	for(auto u:t[x]){
		lab[u] = u;
		dfs(u);
	}

	ll cyc = INF;

	rep(u,1,n)rep(v,1,n)if(!tag[u][v] && lab[u] != lab[v])tomin(cyc,dis[u] + dis[v] + e[u][v]);

	return cyc;
}

int inc[MAXN];

void solve(int x){
	ll cyc = dij(x);

	if(cyc == INF)return;

	memset(inc,0,sizeof inc);

	int a = 0,b = 0;

	rep(u,1,n)rep(v,1,n)if(!tag[u][v] && lab[u] != lab[v] && dis[u] + dis[v] + e[u][v] == cyc){
		for(int p=u;p;p=pre[p])inc[p]=1;
		for(int p=v;p;p=pre[p])inc[p]=1;

		if(u!=x && v!=x)a = lab[u],b = lab[v];
		else{
			if(v==x)swap(u,v);
			a = v,b = lab[v];
		}
		goto END;
	}

	END : ;
	
	assert(a && b);

	rep(u,1,n)if(!inc[u])tomin(ans,cyc + e[x][u]); 
	
	ban[a] = 1;
	tomin(ans,dij(x) + e[x][a]);
	ban[a] = 0;

	ban[b] = 1;
	tomin(ans,dij(x) + e[x][b]);
	ban[b]= 0;
}	

int main(){
	cin>>n>>m;
	rep(i,1,n)rep(j,1,n)e[i][j] = INF;
	rep(i,1,m){
		int u,v,w;cin>>u>>v>>w;
		e[u][v] = e[v][u] = w;
	}
	
	rep(i,1,n){
		solve(i);
	}

	if(ans==INF)cout<<"-1\n";
	else cout<<ans<<endl;

    return 0;
}

Submission Info

Submission Time
Task Ex - Make Q
User Crying
Language C++ (GCC 9.2.1)
Score 625
Code Size 2258 Byte
Status AC
Exec Time 445 ms
Memory 5824 KB

Judge Result

Set Name Sample All AfterContest
Score / Max Score 0 / 0 625 / 625 0 / 0
Status
AC × 3
AC × 67
AC × 2
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 03_random3_04.txt, 03_random3_05.txt, 03_random3_06.txt, 03_random3_07.txt, 03_random3_08.txt, 03_random3_09.txt, 04_killer_00.txt, 04_killer_01.txt, 04_killer_02.txt, 04_killer_03.txt, 04_killer_04.txt, 04_killer_05.txt, 04_killer_06.txt, 04_killer_07.txt, 04_killer_08.txt, 04_killer_09.txt, 05_minmax_00.txt, 05_minmax_01.txt, 06_handmade_00.txt, 06_handmade_01.txt
AfterContest after_contest_00.txt, after_contest_01.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 11 ms 4148 KB
00_sample_01.txt AC 4 ms 4004 KB
00_sample_02.txt AC 5 ms 4172 KB
01_random_00.txt AC 120 ms 5092 KB
01_random_01.txt AC 406 ms 5020 KB
01_random_02.txt AC 409 ms 4992 KB
01_random_03.txt AC 418 ms 5132 KB
01_random_04.txt AC 403 ms 5132 KB
01_random_05.txt AC 415 ms 5164 KB
01_random_06.txt AC 420 ms 5240 KB
01_random_07.txt AC 406 ms 5224 KB
01_random_08.txt AC 414 ms 5120 KB
01_random_09.txt AC 415 ms 5120 KB
01_random_10.txt AC 416 ms 5192 KB
01_random_11.txt AC 414 ms 5008 KB
01_random_12.txt AC 413 ms 4964 KB
01_random_13.txt AC 418 ms 5112 KB
01_random_14.txt AC 421 ms 5124 KB
01_random_15.txt AC 423 ms 5024 KB
01_random_16.txt AC 425 ms 5012 KB
01_random_17.txt AC 441 ms 5140 KB
01_random_18.txt AC 435 ms 5176 KB
01_random_19.txt AC 424 ms 5016 KB
01_random_20.txt AC 426 ms 5024 KB
01_random_21.txt AC 433 ms 5172 KB
01_random_22.txt AC 434 ms 5172 KB
01_random_23.txt AC 443 ms 4996 KB
01_random_24.txt AC 421 ms 4964 KB
01_random_25.txt AC 445 ms 5140 KB
01_random_26.txt AC 432 ms 5068 KB
01_random_27.txt AC 443 ms 5252 KB
01_random_28.txt AC 437 ms 4980 KB
01_random_29.txt AC 432 ms 5100 KB
02_random2_00.txt AC 422 ms 5196 KB
02_random2_01.txt AC 411 ms 5140 KB
02_random2_02.txt AC 431 ms 5000 KB
02_random2_03.txt AC 422 ms 5240 KB
02_random2_04.txt AC 416 ms 5152 KB
02_random2_05.txt AC 429 ms 5252 KB
02_random2_06.txt AC 406 ms 5120 KB
02_random2_07.txt AC 428 ms 5028 KB
02_random2_08.txt AC 409 ms 5112 KB
02_random2_09.txt AC 406 ms 5200 KB
03_random3_00.txt AC 127 ms 5012 KB
03_random3_01.txt AC 118 ms 5116 KB
03_random3_02.txt AC 126 ms 4948 KB
03_random3_03.txt AC 219 ms 5228 KB
03_random3_04.txt AC 236 ms 5104 KB
03_random3_05.txt AC 147 ms 4968 KB
03_random3_06.txt AC 139 ms 5104 KB
03_random3_07.txt AC 230 ms 5116 KB
03_random3_08.txt AC 237 ms 4988 KB
03_random3_09.txt AC 222 ms 4988 KB
04_killer_00.txt AC 431 ms 5040 KB
04_killer_01.txt AC 430 ms 5136 KB
04_killer_02.txt AC 424 ms 5136 KB
04_killer_03.txt AC 411 ms 5100 KB
04_killer_04.txt AC 415 ms 4964 KB
04_killer_05.txt AC 411 ms 5228 KB
04_killer_06.txt AC 430 ms 5036 KB
04_killer_07.txt AC 412 ms 5008 KB
04_killer_08.txt AC 415 ms 5100 KB
04_killer_09.txt AC 428 ms 5036 KB
05_minmax_00.txt AC 4 ms 4208 KB
05_minmax_01.txt AC 389 ms 5084 KB
06_handmade_00.txt AC 4 ms 4004 KB
06_handmade_01.txt AC 4 ms 4164 KB
after_contest_00.txt AC 380 ms 5824 KB
after_contest_01.txt AC 6 ms 4160 KB