Happy new year (shortest path +dfs)

Insert picture description here

Idea: Use the shortest path to preprocess the shortest distance to a relative's house first, and then violently search for the shortest order of visits.

#pragma GCC optimize(2)
#include<bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
typedef pair<int,PII> PIII;
const int mod=1e9+7;
const int N=2e5+5;
const int inf=0x7f7f7f7f;

int gcd(int a,int b)
{
    
    
    return b==0?a:gcd(b,a%b);
}
 
ll lcm(ll a,ll b)
{
    
    
    return a*(b/gcd(a,b));
}
 
template <class T>
void read(T &x)
{
    
    
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    
    
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
         write(x / 10);
    putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
    
    
    ll res=1%p;
    while(b)
    {
    
    
        if(b&1)
            res=res*a%p;
        a=1ll*a*a%p;
        b>>=1;
    }
    return res;
}
int n,m;
struct node
{
    
    
    int nex,w,to;
    /* data */
}edge[N];
int head[N],tot;
int source[N];
int dis[6][N],vis[N];
void add(int u,int v,int w)
{
    
    
    edge[tot].to=v;
    edge[tot].w=w;
    edge[tot].nex=head[u];
    head[u]=tot++;

}

void dij(int star,int dis[])
{
    
    
    memset(dis,inf, N*4);
     dis[star]=0;
    priority_queue<PII,vector<PII>,greater<PII> >q;
    q.push({
    
    0,star});
   
    while(!q.empty())
    {
    
    
        auto now=q.top();
        q.pop();
        int d=now.first,u=now.second;
       // cout<<u<<' ';
        //cout<<vis[u]<<endl;
        if(vis[u])continue;
        vis[u]=1;
        for(int i=head[u];~i;i=edge[i].nex)
        {
    
    
            
            int v=edge[i].to;
            //cout<<v<<' '<<dis[v]<<endl;
            if(dis[v]>edge[i].w+d)
            {
    
    
                dis[v]=edge[i].w+d;
                q.push({
    
    dis[v],v});
            }
        }
    }
    memset(vis,0,sizeof vis);
}
int dfs(int u,int star,int distance )
{
    
    
    if(u==6)return distance;///当前已经拜访完最后一个亲戚
    int res=inf;
    for(int i=1;i<=5;i++)
    {
    
    
       if(!vis[i])
       {
    
    
           int nex=source[i];//要拜访亲戚对应的车站
           vis[i]=1;
           res=min(res,dfs(u+1,i,distance+dis[star][nex]));
           vis[i]=0;
       }
    }
    return res;
    
}
int main()
{
    
    
   scanf("%d%d",&n,&m);
   memset(head,-1,sizeof head);
   source[0]=1;
   for(int i=1;i<6;i++)
   {
    
    
       cin>>source[i];
   }
   while(m--)
   {
    
    
       int u,v,w;
       cin>>u>>v>>w;
       add(u,v,w);
       add(v,u,w);
   }
   for(int i=0;i<6;i++) dij(source[i],dis[i]);
  /* for(int i=0;i<6;i++){
       for(int j=0;j<10;j++)
       cout<<dis[i][j]<<' ';
       cout<<endl;
   }*/
   memset(vis,0,sizeof vis);
   
  printf("%d\n",dfs(1,0,0));

   
   

   
   return 0;

}


Guess you like

Origin blog.csdn.net/qq_43619680/article/details/112712533