Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define nmax 2005
- #define mmax 10005
- using namespace std;
- ifstream fin ("ubuntzei.in");
- ofstream fout ("ubuntzei.out");
- const int pmax = (1 << 17) + 2;
- const int oo=(2e9);
- int C[21], cost[21][21], dist[nmax], dp[pmax][21], inq[nmax];
- priority_queue <pair <int, int> >q;
- vector <pair <int, int> > L[nmax];
- int n, m ,k;
- void Read()
- {
- int x, y, c;
- fin >> n >> m >> k;
- for (int i = 1; i <= k; i++)
- fin >> C[i];
- C[0] = 1;
- C[++k] = n;
- for (int i = 1; i <= m; i++)
- {
- fin >> x >> y >> c;
- L[x].push_back({y,c});
- L[y].push_back({x,c});
- }
- }
- void Dijkstra(int x)
- {
- int nod, cNode, cCost;
- dist[x] = 0;
- q.push({0, x});
- while(!q.empty())
- {
- nod = q.top().second;
- q.pop();
- inq[nod] = 0;
- for (auto i : L[nod])
- {
- cNode = i.first;
- cCost = i.second;
- if(dist[cNode] > dist[nod] + cCost)
- {
- dist[cNode] = dist[nod] + cCost;
- q.push({-dist[cNode], cNode});
- }
- }
- }
- }
- void Initializare()
- {
- for(int i=1; i<=n; i++)
- dist[i] = oo;
- }
- void Solve()
- {
- for (int i = 0; i <= k; i++)
- {
- Initializare();
- Dijkstra(C[i]);
- for (int j = 0; j <= k; j++)
- cost[i][j] = dist[C[j]];
- }
- int mStare = (1 << k);
- for (int stare = 1; stare < mStare; stare++)
- for (int node = 0; node <= k; node++)
- dp[stare][node] = oo;
- for (int node = 0 ; node <= k; node++)
- dp[(1 << node)][node] = cost[0][node];
- for (int stare = 1; stare < mStare; stare++)
- for (int fNode = 0; fNode <= k; fNode++)
- if (stare & (1 << fNode))
- for (int sNode = 0; sNode <= k; sNode++)
- if (!(stare & (1 << sNode)))
- dp[stare | (1 << sNode)][sNode] = min(dp[stare | (1 << sNode)][sNode], dp[stare][fNode] + cost[fNode][sNode]);
- int mn = oo;
- for (int i = 0 ; i < k; i++)
- {
- ///cout << mn << " " << dp[mStare - 1][i] << " " << cost[i][k] << "\n";
- mn = min(mn, dp[mStare - 1][i] + cost[i][k]);
- }
- fout << mn;
- }
- int main()
- {
- Read();
- Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement