Los U84985 Seaway Valley to find a way

Los U84985 Seaway Valley to find a way

Topic background

Seaway S E A w A the y- and his girlfriend upcoming JDFZ J D F the Z- start their high school life ... but, unfortunately, did not let the evil placement test * S Seaway E A w A ** the y-* and girlfriend assigned a class (qwq). Strong Acacia let them decide every day at noon to find each other ......

Title Description

JDFZ J D F. The Z campus contains a N N points, M M weighted undirected graph edges. Seaway S E A W A Y and girlfriend to meet total P P days (. 1 \ Le P \ Le 50001≤ P ≤5000), and, two of them each day position is not determined (it is wonderful). Every day at noon, they certainly want to take a minimum of two-way and will be able to meet each other. But Seaway S E A w A the y- soon encountered a problem: he does not know the way ...... (even Seaway S E A w A the y- not their way, his girlfriend less likely to find their way up) two Luchi how can successfully meet it? Seaway S E A w A the y- inspiration, thought of a way: he wants to write a program to output every day they go out of the path, so that he and his girlfriend could find each other smoothly it! But Seaway SE A w A the y- bother to do such a simple task (in fact, he does not), so he put this task to you.

Input Format

The first row comprises two integers: N N , M M .

Next M M rows, each row of three integers X-, the Y, Z X- , the Y , Z , represents X-, the Y X- , the Y has a length of between Z Z side.

Next is an integer P P , S * represents Seaway E A W A Y ** and girlfriend to meet total P P * days.

The next P P lines of two integers A, B A , B , represents the day of their position A, B A , B .

Output Format

Output is P P rows, each row containing a plurality of integer representing A, B A , B path between the shortest (output contains A, B A , B in ascending order output). If A, B A , B is not the shortest path, between the output qwq.

Sample input and output

Input # 1 copy

Output # 1 copy

Description / Tips

prompt:

S * does not exclude Seaway E A W A Y ** * on a starting situation and the girlfriend.

data range:

1 \ N \ 1001≤ the N ≤100,1 \ M \ the 100001≤ M ≤10000.

answer:

\ (Seaway \) on the basis of graph theory special T2 ...

In fact, the enumeration break with floyd, the final output ans array (to ensure ascending)

If tot (counter) was 0, then output qwq it.

Problems find many details, when the data is also cost me a lot of thought.

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,p,tot,ans[110];
int map[110][110];
int main()
{
    //freopen("#10.in","r",stdin);
    //freopen("#10.out","w",stdout);
    scanf("%d%d",&n,&m);
    memset(map,0x3f,sizeof(map));
    for(int i=1;i<=n;i++)
        map[i][i]=0;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        map[x][y]=map[y][x]=z;
    }
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                map[i][j]=min(map[i][k]+map[k][j],map[i][j]);
    scanf("%d",&p);
    while(p--)
    {
        tot=0;
        memset(ans,0,sizeof(ans));
        int a,b;
        scanf("%d%d",&a,&b);
        for(int k=1;k<=n;k++)
            if(map[a][k]+map[k][b]==map[a][b] || map[b][k]+map[k][a]==map[a][b])
                ans[++tot]=k;
        if(tot==0)
        {
            printf("qwq\n");
            continue;
        }
        for(int i=1;i<=tot;i++)
            printf("%d ",ans[i]);
        printf("\n");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11402139.html