Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <algorithm>
- #include <unordered_set>
- #include <queue>
- #define task ""
- using namespace std;
- using ll = long long;
- using ld = long double;
- const int N = 2e3 + 2;
- struct way{
- int v, w;
- way() {}
- way(int x, int y){
- v = x;
- w = y;
- }
- };
- vector<way> adj[N];
- int goods[N];
- int n, m, k;
- bool ck[12];
- int ans(1e9);
- int res[N];
- void Read(){
- cin >> n >> m >> k;
- for(int i = 1; i <= n; ++i){
- int k;
- cin >> k;
- while(k--){
- int v;
- cin >> v;
- goods[i] |= 1 << v;
- }
- }
- for(int i = 1; i <= m; ++i){
- int u, v, w;
- cin >> u >> v >> w;
- adj[u].push_back(way(v, w));
- adj[v].push_back(way(u, w));
- }
- }
- int Dijkstra(int &a){
- vector<vector<bool>> ck(N, vector<bool>(N));
- vector<int> ans(N);
- fill(ans.begin(), ans.end(), 12);
- struct Tque{
- int v, w, a;
- Tque () {}
- Tque(int m, int n, int p){
- v = m; w = n; a = p;
- }
- bool operator < (const Tque &x) const {
- return w > x.w ||
- (w == x.w && __builtin_popcount(a) > __builtin_popcount(x.a));
- }
- };
- a &= ~goods[1];
- priority_queue<Tque> s;
- s.push(Tque(1, 0, a));
- while(s.size()){
- Tque c = s.top();
- s.pop();
- if(c.v == n && c.a == 0)
- return c.w;
- if(ck[c.v][c.a] || __builtin_popcount(c.a) > ans[c.v])
- continue;
- ck[c.v][c.a] = 1;
- ans[c.v] = __builtin_popcount(c.a);
- for(auto i : adj[c.v]){
- int aa = c.a;
- aa &= ~goods[i.v];
- if(!ck[i.v][aa] && __builtin_popcount(aa) <= ans[i.v])
- s.push(Tque(i.v, c.w + i.w, aa));
- }
- }
- }
- void Solve(){
- int I = 0, S = 0;
- for(int i = 1; i <= k; ++i)
- if(ck[i])
- I |= 1 << i;
- res[I] = Dijkstra(I);
- }
- void Update_min(){
- int I = 0, S = 0;
- for(int i = 1; i <= k; ++i)
- if(ck[i])
- I |= 1 << i;
- else
- S |= 1 << i;
- I &= ~goods[1];
- S &= ~goods[1];
- ans = min(ans, max(res[I], res[S]));
- }
- void dfs(int i, bool check){
- if(i > k){
- if(check)
- Solve();
- else
- Update_min();
- return;
- }
- if(goods[1] & (1 << i))
- goto done;
- ck[i] = 1;
- dfs(i + 1, check);
- ck[i] = 0;
- done:;
- dfs(i + 1, check);
- }
- main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0); if(fopen(task".INP", "r"))
- freopen(task".INP", "r", stdin),
- freopen(task".OUT", "w", stdout);
- Read();
- dfs(1, 1);
- dfs(1, 0);
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment