#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
using namespace std;
const int maxn = int(1e5)+5, maxk = 6, inf = int(1e9)+5;
int n, D[maxk][maxn], dp[(1<<maxk)][maxn], A[maxk];
vector<pair<int, int>> graph[maxn];
void dijkstra(int type, int node)
{
for(int i = 0;i < n;i++) D[type][i] = inf;
D[type][node] = 0;
set<pair<int, int>> Q;
Q.insert({D[type][node], node});
while(!Q.empty())
{
pair<int, int> top = *Q.begin();
Q.erase(Q.begin());
for(auto it: graph[top.second])
{
if(D[type][it.first] > D[type][top.second]+it.second)
{
if(D[type][it.first] != inf) Q.erase({D[type][it.first], it.first});
D[type][it.first] = D[type][top.second]+it.second;
Q.insert({D[type][it.first], it.first});
}
}
}
}
int main(void)
{
int k, m, u, v, w;
scanf("%d%d%d", &n, &k, &m);
for(int i = 0;i < k;i++)
{
scanf("%d", &A[i]);
A[i]--;
}
for(int i = 0;i < m;i++)
{
scanf("%d%d%d", &u, &v, &w);
u--, v--;
graph[u].push_back({v, w}), graph[v].push_back({u, w});
}
for(int i = 0;i < k;i++) dijkstra(i, A[i]);
for(int mask = 1;mask < (1<<k);mask++)
{
set<pair<int, int>> Q;
for(int i = 0;i < n;i++)
{
dp[mask][i] = inf;
for(int j = 0;j < k;j++)
{
if((mask&(1<<j))) dp[mask][i] = min(dp[mask][i], dp[(mask^(1<<j))][i]+D[j][i]);
}
Q.insert({dp[mask][i], i});
}
while(!Q.empty())
{
pair<int, int> top = *Q.begin();
Q.erase(Q.begin());
for(auto it: graph[top.second])
{
if(dp[mask][it.first] > dp[mask][top.second]+it.second)
{
Q.erase({dp[mask][it.first], it.first});
dp[mask][it.first] = dp[mask][top.second]+it.second;
Q.insert({dp[mask][it.first], it.first});
}
}
}
}
int res = inf;
for(int i = 0;i < n;i++) res = min(res, dp[(1<<k)-1][i]);
printf("%d\n", res);
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &k, &m);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
cities.cpp:43:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
cities.cpp:49:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &u, &v, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2792 KB |
Output is correct |
3 |
Incorrect |
3 ms |
2792 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
421 ms |
21800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
21800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
664 ms |
29868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1205 ms |
40480 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |