Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : _example
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 15 February 2023 [21:10]
- */
- #include<bits/stdc++.h>
- using namespace std;
- struct A{
- int currentNode,weight,mask;
- bool operator < (const A&o) const{
- return weight>o.weight;
- }
- };
- struct B{
- int nextNode,edgeWeight;
- };
- vector<B > g[20];
- int dis[20][33000];
- priority_queue<A > heap;
- int floydWarshall[210][210];
- int item[210],newItem[20];
- int convertNum[210];
- int main(){
- int n,m,k;
- scanf("%d %d %d",&n,&m,&k);
- memset(item,-1,sizeof item);
- for(int i=0;i<k;i++){
- int num;
- scanf("%d",&num);
- item[num] = i;
- }
- // Minimize graph
- for(int i=1;i<=n;i++)
- for(int j=1;j<=n;j++)
- floydWarshall[i][j] = 1e9;
- while(m--){
- int u,v,w;
- scanf("%d %d %d",&u,&v,&w);
- floydWarshall[u][v] = floydWarshall[v][u] = min(floydWarshall[u][v],w);
- }
- for(int k=1;k<=n;k++)
- for(int i=1;i<=n;i++)
- for(int j=1;j<=n;j++)
- floydWarshall[i][j] = min(floydWarshall[i][j],floydWarshall[i][k]+floydWarshall[k][j]);
- // create new graph
- vector<int > nodeList;
- int cnt = 0;
- memset(newItem,-1,sizeof newItem);
- for(int i=1;i<=n;i++){
- if(i == 1 || i == n || item[i] != -1){
- nodeList.push_back(i);
- convertNum[i] = ++cnt;
- if(item[i] != -1) newItem[cnt] = item[i];
- }
- }
- for(auto x:nodeList){
- for(auto y:nodeList){
- if(x == y) continue;
- g[convertNum[x]].push_back({convertNum[y],floydWarshall[x][y]});
- g[convertNum[y]].push_back({convertNum[x],floydWarshall[x][y]});
- }
- }
- // Dijkstra
- for(int i=1;i<=cnt;i++)
- for(int j=0;j<(1<<k);j++)
- dis[i][j] = 1e9;
- if(newItem[1] == -1) dis[1][0] = 0,heap.push({1,0,0});
- else dis[1][1<<newItem[1]] = 0,heap.push({1,0,1<<newItem[1]});
- while(!heap.empty()){
- A now = heap.top();
- heap.pop();
- if(now.currentNode == cnt && now.mask == (1<<k)-1){
- printf("%d\n",now.weight);
- return 0;
- }
- for(auto x:g[now.currentNode]){
- int nmask = now.mask;
- if(newItem[x.nextNode] != -1) nmask |= (1<<newItem[x.nextNode]);
- if(dis[x.nextNode][nmask] > now.weight + x.edgeWeight){
- dis[x.nextNode][nmask] = now.weight + x.edgeWeight;
- heap.push({x.nextNode,now.weight + x.edgeWeight,nmask});
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment