#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
//Cities oj.uz not nkbuild spoj(khác dữ kiệu)
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 = 2e5 + 7;
const int M = 35;
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(const 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';
exit(0);
}
for(plli adj: a[p.X])
{
c = adj.X + f[p.X][p.Y];
mark = p.Y;
if(city[adj.Y]) mark |= Bit(city[adj.Y] - 1);
if(f[adj.Y][mark] > c)
{
f[adj.Y][mark] = c;
s.insert({c, {adj.Y, mark}});
}
for(int bit: b[p.Y])
{
cost = c + f[adj.Y][bit];
if(f[adj.Y][mark | bit] > cost)
{
f[adj.Y][mark | bit] = cost;
s.insert({cost, {adj.Y, 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 |
56 ms |
59768 KB |
Output is correct |
2 |
Correct |
47 ms |
59880 KB |
Output is correct |
3 |
Correct |
48 ms |
59960 KB |
Output is correct |
4 |
Correct |
49 ms |
60084 KB |
Output is correct |
5 |
Correct |
60 ms |
60084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3291 ms |
125704 KB |
Output is correct |
2 |
Correct |
3379 ms |
125704 KB |
Output is correct |
3 |
Correct |
357 ms |
125704 KB |
Output is correct |
4 |
Correct |
228 ms |
125704 KB |
Output is correct |
5 |
Correct |
810 ms |
125704 KB |
Output is correct |
6 |
Correct |
129 ms |
125704 KB |
Output is correct |
7 |
Correct |
59 ms |
125704 KB |
Output is correct |
8 |
Correct |
51 ms |
125704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
125704 KB |
Output is correct |
2 |
Correct |
75 ms |
125704 KB |
Output is correct |
3 |
Correct |
56 ms |
125704 KB |
Output is correct |
4 |
Correct |
71 ms |
125704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6055 ms |
230212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6034 ms |
252016 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |