1 条题解
-
0
思路
大图先从家及五亲戚站跑六次Dijkstra,再枚举5!个访问顺序求度量闭包路线;允许途中经过其他亲戚,终点可任意且不需返家,不能直接在5万站上枚举路线。
参考程序(C++11)
#include <bits/stdc++.h> using namespace std; int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n,m;cin>>n>>m;array<int,6>special;special[0]=0;for(int i=1;i<=5;i++){cin>>special[i];special[i]--;}vector<vector<pair<int,int>>>g(n);for(int i=0;i<m;i++){int u,v,w;cin>>u>>v>>w;--u;--v;g[u].push_back({v,w});g[v].push_back({u,w});}vector<vector<int>>distance(6,vector<int>(n,1000000000));for(int i=0;i<6;i++){auto &d=distance[i];priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;d[special[i]]=0;q.push({0,special[i]});while(!q.empty()){auto p=q.top();q.pop();int v=p.second;if(p.first!=d[v])continue;for(auto e:g[v])if(d[e.first]>d[v]+e.second){d[e.first]=d[v]+e.second;q.push({d[e.first],e.first});}}}array<int,5>order={1,2,3,4,5};long long best=LLONG_MAX;do{long long sum=0;int previous=0;for(int i:order){sum+=distance[previous][special[i]];previous=i;}best=min(best,sum);}while(next_permutation(order.begin(),order.end()));cout<<best<<'\n';}
- 1