Submission #544949

# Submission time Handle Problem Language Result Execution time Memory
544949 2022-04-03T08:10:46 Z MilosMilutinovic Cities (BOI16_cities) C++14
100 / 100
2619 ms 42812 KB
#include<bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template<typename T> bool chkmin(T&x,T y){return x<y?x=y,1:0;}
template<typename T> bool chkmax(T&x,T y){return x>y?x=y,1:0;}

const ll inf=(ll)1e18;
int n,k,m,id[100005];
ll dist[100005][1<<5];
vector<pii> g[100005];
priority_queue<pair<ll,int>> que;

void dijkstra(int mask){
	for(int i=1;i<=n;i++)if(dist[i][mask]!=inf)que.push(mp(-dist[i][mask],i));
	while(!que.empty()){
		ll d=-que.top().first;
		int u=que.top().second;
		que.pop();
		if(dist[u][mask]<d)continue;
		for(auto e:g[u]){
			int v=e.first,w=e.second;
			if(dist[v][mask]>dist[u][mask]+w){
				dist[v][mask]=dist[u][mask]+w;
				que.push(mp(-dist[v][mask],v));
			}
		}
	}
}

int main(){
	scanf("%d%d%d",&n,&k,&m);
	for(int i=1;i<=n;i++)id[i]=-1;
	for(int i=1;i<=n;i++)for(int j=0;j<(1<<k);j++)dist[i][j]=inf;
	for(int i=1;i<=k;i++){
		int x;scanf("%d",&x);
		id[x]=i-1; dist[x][1<<(i-1)]=0;
	}
	for(int i=1;i<=m;i++){
		int u,v,w;scanf("%d%d%d",&u,&v,&w);
		g[u].pb(mp(v,w)); g[v].pb(mp(u,w));
	}
	for(int mask=1;mask<(1<<k);mask++){
		if(__builtin_popcount(mask)>1){
			for(int s=((mask-1)&mask);s>0;s=((s-1)&mask)){
				for(int i=1;i<=n;i++)dist[i][mask]=min(dist[i][mask],dist[i][s]+dist[i][mask^s]);
			}
		}
		dijkstra(mask);
	}
	ll ans=inf;
	for(int i=1;i<=n;i++)ans=min(ans,dist[i][(1<<k)-1]);
	printf("%lld",ans);
	return 0;
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:42:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |  scanf("%d%d%d",&n,&k,&m);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~
cities.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |   int x;scanf("%d",&x);
      |         ~~~~~^~~~~~~~~
cities.cpp:50:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |   int u,v,w;scanf("%d%d%d",&u,&v,&w);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 581 ms 38484 KB Output is correct
2 Correct 580 ms 39656 KB Output is correct
3 Correct 348 ms 34716 KB Output is correct
4 Correct 65 ms 8892 KB Output is correct
5 Correct 298 ms 39980 KB Output is correct
6 Correct 60 ms 8988 KB Output is correct
7 Correct 4 ms 3028 KB Output is correct
8 Correct 3 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3028 KB Output is correct
2 Correct 6 ms 3028 KB Output is correct
3 Correct 4 ms 2900 KB Output is correct
4 Correct 4 ms 2880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1248 ms 38628 KB Output is correct
2 Correct 1250 ms 42424 KB Output is correct
3 Correct 828 ms 35412 KB Output is correct
4 Correct 694 ms 27380 KB Output is correct
5 Correct 145 ms 14344 KB Output is correct
6 Correct 67 ms 12600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2612 ms 38504 KB Output is correct
2 Correct 2619 ms 42812 KB Output is correct
3 Correct 2602 ms 42428 KB Output is correct
4 Correct 1841 ms 35500 KB Output is correct
5 Correct 1361 ms 27496 KB Output is correct
6 Correct 234 ms 14312 KB Output is correct
7 Correct 77 ms 12600 KB Output is correct