Submission #538174

#TimeUsernameProblemLanguageResultExecution timeMemory
53817479brueMountains and Valleys (CCO20_day1problem3)C++14
0 / 25
5 ms696 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n, m; int dist[22][22]; int DP[1048576][20]; vector<int> link[5002]; queue<pair<int, int> > que; bool visited[5002]; int main(){ scanf("%d %d", &n, &m); if(n>3){ for(int i=1; i<=m; i++){ int x, y, c; scanf("%d %d %d", &x, &y, &c); if(c==1){ link[x].push_back(y); link[y].push_back(x); } } que.push(make_pair(1, 0)); visited[1] = 1; int nxt = 0, dst=0; while(!que.empty()){ auto tmp = que.front(); que.pop(); nxt = tmp.first; dst = tmp.second; for(auto x: link[tmp.first]){ if(visited[x]) continue; visited[x] = 1; que.push(make_pair(x, tmp.second+1)); } } memset(visited, 0, sizeof(visited)); nxt = dst = 0; que.push(make_pair(nxt, 0)); visited[nxt] = 1; while(!que.empty()){ auto tmp = que.front(); que.pop(); nxt = tmp.first; dst = tmp.second; for(auto x: link[tmp.first]){ if(visited[x]) continue; visited[x] = 1; que.push(make_pair(x, tmp.second+1)); } } printf("%d", 2*n-2-dst); return 0; } for(int i=0; i<=n; i++){ for(int j=0; j<=n; j++){ dist[i][j] = 1e9; } } for(int i=1; i<=m; i++){ int x, y, c; scanf("%d %d %d", &x, &y, &c); dist[x][y] = dist[y][x] = c; } for(int k=0; k<n; k++){ for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } for(int i=0; i<(1<<n); i++) for(int j=0; j<=n; j++) DP[i][j] = 1e9; for(int i=0; i<n; i++) DP[(1<<i)][i] = 0; for(int d=1; d<(1<<n); d++){ for(int i=0; i<n; i++){ if((d>>i)&1){ // printf("%d %d: %d\n", d, i, DP[d][i]); for(int j=0; j<n; j++){ DP[(d|(1<<j))][j] = min(DP[(d|(1<<j))][j], DP[d][i] + dist[i][j]); } } } } int ans = 1e9; for(int i=0; i<n; i++) ans = min(ans, DP[(1<<n)-1][i]); printf("%d", ans); }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:20:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |             scanf("%d %d %d", &x, &y, &c);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         scanf("%d %d %d", &x, &y, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...