Question 221. 2022 Winter Holiday Ladder Competition Training-7-10 Internet Celebrity Check-in Strategy (25 points)


Question 221. 2022 Winter Holiday Ladder Competition Training-7-10 Internet Celebrity Check-in Strategy (25 points)


1. The topic

insert image description here
insert image description here
insert image description here

2. Problem solving

1. Note that the requirement of the strategy is to go from home to home, and you can only go to the Internet celebrity scenic spots halfway, and each Internet celebrity scenic spot has to go, so the key point is to judge whether the starting and ending points of the given input are adjacent to the home, and whether the vis has already been vis. Points, whether the number of vis points is equal to the number of Internet celebrity attractions.
2. For the strategy that meets the requirements, calculate the total cost on the road and then continuously update the minimum cost and strategy number as the result.

#include <bits/stdc++.h>

using namespace std;

const int Inf=0x3f3f3f3f;

int N,M;
int G[201][201];

int main()
{
    
    
    fill(G[0],G[0]+201*201,Inf);
    cin>>N>>M;
    for(int i=0; i<M; i++)
    {
    
    
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        G[u][v]=w;
        G[v][u]=w;
    }
    int K;
    cin>>K;
    int num=0;
    int minval=INT_MAX,res;
    for(int i=1; i<=K; i++)
    {
    
    
        int n;
        scanf("%d",&n);
        int path[202],vis[201];
        path[0]=0,path[n+1]=0;
        fill(vis,vis+201,0);
        int val=0;
        int flag=1;
        int j,count0=0;
        for(j=1; j<=n; j++)
        {
    
    
            scanf("%d",&path[j]);
            if(vis[path[j]])
            {
    
    
                flag=0;
            }
            else
            {
    
    
                vis[path[j]]=1;
                count0++;
            }
            if(G[path[j-1]][path[j]]==Inf)
            {
    
    
                flag=0;
            }
            else
            {
    
    
                val+=G[path[j-1]][path[j]];
            }
        }
        if(G[path[j-1]][path[j]]==Inf)
        {
    
    
            flag=0;
        }
        else
        {
    
    
            val+=G[path[j-1]][path[j]];
        }
        if(count0<N)
        {
    
    
            flag=0;
        }
        if(flag)
        {
    
    
            num++;
            if(val<minval)
            {
    
    
                minval=val;
                res=i;
            }
        }
    }
    cout<<num<<endl;
    cout<<res<<" "<<minval;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324345691&siteId=291194637