The dream of TT

Topic: TT's dream: TT
: This night, TT had a beautiful dream!

In his dream, TT's wish came true, and he became the leader of Meow Star! There are N commercial cities on the Meow Star, numbered 1 to N, of which City 1 is the city where TT is located, that is, the capital.

There are M directional roads on Meow Star for commercial cities to communicate with each other. However, with the increasing prosperity of Meowstar's business, some roads have become very crowded. While TT was distressed, his magic kitty came up with a solution! TT is pleased to accept and promulgate a new policy for this program.

The specific policy is as follows: mark a positive integer for each commercial city to indicate its prosperity. When each cat walks from one commercial city to another commercial city along the road, TT will charge them (destination prosperity-departure point Prosperity) ^ 3 tax.

TT intends to test whether this policy is reasonable, so he wants to know how much tax must be paid to travel from the capital to other cities. If the total amount is less than 3 or cannot be reached, please quietly type '?'.

Input:
Enter T in the first line, indicating that there are T sets of data. (1 <= T <= 50)

For each set of data, enter N in the first row to indicate the number of points. (1 <= N <= 200)

Enter N integers in the second line, which represents the weight a [i] from 1 to N points. (0 <= a [i] <= 20)

Enter M in the third line to indicate the number of directional roads. (0 <= M <= 100000)

In the next M rows, each row has two integers AB, indicating that there is a directed road from A to B.

Next, an integer Q is given, indicating the number of inquiries. (0 <= Q <= 100000)

Each inquiry gives a P, which means seeking the minimum tax from point 1 to point P.

Output:
One line is output for each inquiry. If it is unreachable or the tax is less than 3, then output "?".

Example:
Input:
2
5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5
10
1 2 4 4 5 6 7 8 9 10
10
1 2
2 3
3 1
1 4
4 5
. 6. 5
. 6. 7
. 7. 8
. 8. 9
. 9 10
2
. 3 10
output:
Case. 1:
. 3
. 4
Case 2:
?
?

Problem solving idea: If there is no negative loop, this is a shortest source path dij to solve the problem, but if there is a negative loop, it must still be solved with spfa with better complexity; determine whether the path length to this point is> = points, If there is, mark it and start dfs from this point, all the points found are marked.

Code:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cstring>
#define Max 1000000000
using namespace std;
struct node
{
    int to,next,w;
}e[200005];
int tot=0,head[200005],d[200005],inq[200005],cnt[200005],m,n,dot[200005],vis[200005];
int xunwen[200005];
void add(int x,int y,int w)//前向星
{
    e[++tot].to=y;
    e[tot].next=head[x];
    e[tot].w=w;
    head[x]=tot;
}
void dfs(int v)//dfs
{
    for(int i=head[v];i;i=e[i].next)
    {
        int t=e[i].to;
        if(!vis[t])
        {
            vis[t]=1;
            dfs(t);
        }
    }
}
void spfa()//spfa
{
    for(int i=1;i<=n;i++)
    {
        d[i]=Max;
    }
    d[1]=0;
    inq[1]=1;
    queue<int> q;
    q.push(1);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        inq[u]=0;
        for(int i=head[u];i;i=e[i].next)
        {
            int v=e[i].to,w=e[i].w;
            if(vis[v])//如果这个点已经被标记,不管他;
            {
                continue;
            }
            if(d[v]>d[u]+w)
            {
                d[v]=d[u]+w;
                cnt[v]=cnt[u]+1;//判断一下,如果是的话,打上标记直接dfs
                if(cnt[v]>=n)
                {
                    dfs(v);
                    vis[v]=1;
                }
                if(!inq[v])
                {
                    inq[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    int p;
    scanf("%d",&p);
    int k=0;
    while(p--)
    {
        k++;
        memset(head,0,sizeof(head));
        memset(inq,0,sizeof(inq));
        memset(cnt,0,sizeof(cnt));
        memset(dot,0,sizeof(dot));
        memset(vis,0,sizeof(vis));
        memset(xunwen,0,sizeof(xunwen));
        tot=0;
        scanf("%d",&n);
        int t;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&dot[i]);
        }
        scanf("%d",&m);
        int x,y;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y,(dot[y]-dot[x])*(dot[y]-dot[x])*(dot[y]-dot[x]));
        }
        spfa();
        int Q,p;
        scanf("%d",&Q);
        int l=0;
        while(Q--)
        {
            scanf("%d",&p);
            xunwen[l++]=p;
        }
        printf("Case %d:\n",k);
        for(int i=0;i<l;i++)//判定输出结果就行了
        {
            int r=xunwen[i];
            if(d[r]==Max||vis[r]||d[r]<3)
            {
                printf("?\n");
            }else
            {
                printf("%d\n",d[r]);
            }
        }
    }
}
PGZ
Published 34 original articles · Likes0 · Visits 866

Guess you like

Origin blog.csdn.net/qq_43653717/article/details/105291869
TT