从1出发访问 5个给定点,最小化路程
枚举5个点的排列,然后单源最短路
#include <iostream> #include <cstring> #include <queue> using namespace std ; struct T{ int y,z; T(int y0,int z0){ y=y0,z=z0; } friend bool operator<(T x,T y){ return x.z>y.z; } }; const int N=1e6,M=1e6; int n,m,vis[N],d[8][N]; int nxt[M],w[M],go[M],hd[N],all; int ans; void add(int x,int y,int z){ go[++all]=y,nxt[all]=hd[x]; hd[x]=all,w[all]=z; } int house[8]; void dijk(int k,int v0){ int i,x,y,z; priority_queue<T> q; for(i=1;i<=n;i++) d[k][i]=1e9,vis[i]=0; d[k][v0]=0; q.push(T(v0,0)); while(q.empty()==0){ x=q.top().y,q.pop(); if(vis[x]) continue; vis[x]=1; for(i=hd[x];i;i=nxt[i]){ y=go[i],z=w[i]; if(d[k][x]+z<d[k][y]){ d[k][y]=d[k][x]+z; q.push(T(y,d[k][y])); } } } } int b[8]; void dfs(int cnt,int last,int s){ if(cnt>5){ ans=min(ans,s);return; } for(int i=1;i<=5;i++){ if(b[i])continue; b[i]=1; dfs(cnt+1,i,s+d[last][house[i]]); b[i]=0; } } signed main(){ int i,x,y,z; cin>>n>>m; for(i=1;i<=5;i++) cin>>house[i]; for(i=1;i<=m;i++) cin>>x>>y>>z,add(x,y,z),add(y,x,z); dijk(0,1); for(i=1;i<=5;i++) dijk(i,house[i]); ans=1e9; dfs(1,0,0); cout<<ans; }
标签:int,10078,add,新年好,3.2,ans,include,hd From: https://www.cnblogs.com/towboa/p/16883478.html