#include<bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define FileName "test"
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define X first
#define Y second
//#define Karma
using namespace std;
template <typename T> inline void Cin(T &x)
{
char c;
T sign = 1;
x = 0;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
if (c == '-')
sign = -1;
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
x *= sign;
}
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
const int N = 1e5 + 7;
const int M = (1 << 6);
const ll oo = 1e15 + 1;
int n, k, x, city[N], m, u, v, mask, mark;
set<pair<ll, pair<int, int>>> s;
ll f[N][M], c, w, cost;
vector<plli> a[N];
vector<int> b[M];
//f[u][mark] = f[v][mark] + c(u, v)
//f[u][mark] = f[u][mark'] + f[u][mark ^ mark']
int Bit(int i)
{
return (1 << i);
}
void Enter()
{
Cin(n); Cin(k); Cin(m);
For(i, 0, Bit(k) - 1)
{
For(j, 0, Bit(k) - 1)
{
if((i & j) == 0)
b[i].pb(j);
}
}
fill_n(&f[0][0], N * M, oo);
For(i, 1, k)
{
Cin(x);
city[x] = i;
f[x][Bit(i - 1)] = 0;
s.insert({0, {x, Bit(i - 1)}});
}
For(i, 1, m)
{
Cin(u);
Cin(v);
Cin(c);
a[u].pb({c, v});
a[v].pb({c, u});
}
}
void Solve()
{
while(!s.empty())
{
w = s.begin()->X;
plli p = s.begin()->Y;
s.erase(s.begin());
if(w > f[p.X][p.Y])
continue;
if(p.Y == Bit(k) - 1)
{
cout << w << '\n';
return;
}
u = p.X;
mask = p.Y;
for(plli adj: a[u])
{
c = adj.X + f[u][mask];
v = adj.Y;
mark = mask;
if(city[v])
mark |= Bit(city[v] - 1);
if(f[v][mark] > c)
{
f[v][mark] = c;
s.insert({c, {v, mark}});
}
for(int bit: b[mask])
{
cost = c + f[v][bit];
if(f[v][mark | bit] > cost)
{
f[v][mark | bit] = cost;
s.insert({cost, {v, mark | bit}});
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef Karma
freopen(FileName".inp", "r", stdin);
freopen(FileName".out", "w", stdout);
#endif // Karma
Enter();
Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
52728 KB |
Output is correct |
2 |
Correct |
53 ms |
52836 KB |
Output is correct |
3 |
Correct |
60 ms |
52912 KB |
Output is correct |
4 |
Correct |
58 ms |
52948 KB |
Output is correct |
5 |
Correct |
52 ms |
53036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3243 ms |
114444 KB |
Output is correct |
2 |
Correct |
3020 ms |
114444 KB |
Output is correct |
3 |
Correct |
263 ms |
114444 KB |
Output is correct |
4 |
Correct |
202 ms |
114444 KB |
Output is correct |
5 |
Correct |
791 ms |
114444 KB |
Output is correct |
6 |
Correct |
120 ms |
114444 KB |
Output is correct |
7 |
Correct |
53 ms |
114444 KB |
Output is correct |
8 |
Correct |
47 ms |
114444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
114444 KB |
Output is correct |
2 |
Correct |
79 ms |
114444 KB |
Output is correct |
3 |
Correct |
51 ms |
114444 KB |
Output is correct |
4 |
Correct |
59 ms |
114444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6108 ms |
200040 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6107 ms |
232224 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |