Advertisement
Guest User

Untitled

a guest
Nov 5th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4.     int n,m,k;
  5.     cin>>n>>m>>k;
  6.     vector<vector<pair<int,int>>> edges(n);
  7.     vector<int> fish(n);
  8.     vector<int> goal(1<<k,-1);
  9.     vector<vector<bool> > visited(n, vector<bool>(1<<k,false));
  10.     for(int i=0;i<n;i++) {
  11.         int t;
  12.         cin>>t;
  13.         for(int j=0;j<t;j++) {
  14.             int f;
  15.             cin>>f;
  16.             fish[i] |= 1<<(f-1);
  17.         }
  18.     }
  19.     for(int j=0;j<m;j++) {
  20.         int a,b,c;
  21.         cin>>a>>b>>c;a--;b--;
  22.         edges[a].push_back(make_pair(b,c));
  23.         edges[b].push_back(make_pair(a,c));
  24.     }
  25.  
  26.     priority_queue<tuple<int,int,int>> q;
  27.     q.push(make_tuple(0,0,fish[0]));
  28.     while(!q.empty()) {
  29.         int shop,mask,cost;
  30.         tie(cost,shop,mask) = q.top();
  31.         q.pop();
  32.         if(visited[shop][mask]) continue;
  33.         if(shop==n-1) goal[mask] = -cost;  
  34.         visited[shop][mask] = true;
  35.         for(auto edge : edges[shop]) {
  36.             q.push(make_tuple(cost - edge.second, edge.first, mask | fish[edge.first]));
  37.         }
  38.     }
  39.     int best = INT_MAX;
  40.     for(int i=0;i<(1<<k);i++) {
  41.         for(int j=0;j<(1<<k);j++) {
  42.             if((i|j) == (1<<k)-1 && goal[i]>=0 && goal[j]>=0) {
  43.                 best = min(best,max(goal[i],goal[j]));
  44.             }
  45.         }
  46.     }
  47.     cout<<best<<endl;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement