Luogu p1364 problem solving

This is a widely searched topic. The topic itself is not difficult. The basic idea is: first define a structure. There are 4 parameters in the structure. The first one represents the number of people, the second one represents the left child of the node, and the other represents the right child of the node. child. The last one represents the parent node of this node. Note that this parent node is not input, but obtained. Define a function to find the parent of each node. Then use a for loop to traverse each point, set it as the starting node, and then enter the queue, exit the pair, and expand the node in turn  

code show as below

#include<bits/stdc++.h>
using namespace std;
int n;
int cont=INT_MAX;
struct node
{
    int s,z,y,f; //z means left child y means right child f means parent node s means number of people
}a[110];
struct Node
{
    int r1,k1,s1; //s1 represents the population number k1 represents the distance from the starting point r1 represents the array subscript
    Node(int r1,int k1,int s1):r1(r1) ,k1(k1),s1(s1){};
};
int visited[110];
int sum=0;
queue< Node >q;
void zhaofu() //Function to find the parent node
{
    for(int i=1 ;i<=n;i++)
    {
        if(a[i].z)
            a[a[i].z].f=i;
        if(a[i].y)
            a[a[i].y] .f=i;
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i].s>>a[i].z>>a[i].y;
    zhaofu();
    for(int i=1;i<=n;i++)
    {
        memset(visited,0,sizeof(visited));
        q.push(Node(i,0,a[i].s));
        while (!q.empty())
        {
            Node p=q.front();
            visited[p.r1]=1;
            int i1=a[p.r1].z;
            if(i1&&!visited[i1])
            {
                q.push(Node(i1,p.k1+1,a[i1].s));
                sum+=(a[i1].s)*(p.k1+1);
            }
            int i2=a[p.r1].y;
            if(i2&&!visited[i2])
            {
                q.push(Node(i2,p.k1+1,a[i2].s));
                sum+=(a[i2].s)*(p.k1+1);
            }
            int i3=a[p.r1].f;
            if(i3&&!visited[i3])
            {
                q.push(Node(i3,p.k1+1,a[i3].s));
                sum+=(a[i3].s)*(p.k1+1);
            }
            q.pop();
        }
        cont=min(cont,sum);
        sum=0;
    }
    cout<<cont<<endl;

}

Woohoo, I passed 4 test points in the first handover, but I didn't pass the last one. I don't know why. After downloading the test sample, after thinking hard, I finally found the problem. It turned out to be the initial value of cont in my code. The setting is too small (I set it to 9999 at the beginning. When the number of nodes is 100 groups, the calculated number is larger than 9999. At this time, the smallest value becomes 9999, which is obviously wrong for QAQ) and then from the Internet Find out how to set max min min. In the end, happy O(∩_∩)O~~

Guess you like

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