C - the basic embodiment of FIG three basic storage (including fast row structure)

Description

Graph theory to solve the problem, we must first think about what kind of a stored map. But small Xin but how could not understand how the memory map can help solve the problem. Can you help him solve this problem?
Input

Multiple sets of input to the end of the file.
The first line of each group of two numbers n, m denotes n points, m article directed edge. Then there are m rows of two numbers u, v, w has a representative of u to v has a value of w to the right side. M + 2 second row has a number q represents the number of interrogation, then q lines each have a query, a number of a input

Note: point number is 0 ~ n-1,2 <= n <= 500000, 0 <= m <= 500000,0 <= q <= 500000, u = v, w is an int!. Input and to ensure that no self-loop edge weight
Output

For each query, the output line of two numbers x, y. It denotes a side of the sorted article is x to y. For each edge collation is as follows:
the weight of a small front.
Small edge weights equal to the previous starting point number
weights and reaches the starting point of point number equal small front
NOTE: edge number 0 from the start
Sample

Input

4 3
0 1 1
1 2 2
1 3 0
3
0
1
2
Output

1 3
0 1
1 2

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
struct node
{
    int u,v,w;
}head[500000];//数据量大时无法用二维数组储存,使用结构体
void kuaipai(int l,int r)
{
    int i = l,j = r;
    struct node k;
    k = head[i];
    if(l>=r)
        return ;
    while(i<j)
    {
        while(i<j&&(k.w<head[j].w||(k.w==head[j].w&&k.u<head[j].u)||(k.w==head[j].w&&k.u==head[j].u&&k.v<head[j].v)))
            j--;
        head[i] = head[j];
        while(i<j&&(k.w>head[i].w||(k.w == head[i].w&&k.u>head[i].u)||(k.w==head[i].w&&k.u==head[i].u&&k.v>head[i].v)))
            i++;
        head[j] = head[i];
    }
    head[i] = k;
    kuaipai(l,i-1);
    kuaipai(i+1,r);
}
int main()
{
    int n,m,i;
    while(~scanf("%d %d",&n,&m))
    {
        /*for( i = 0; i < n; i++)
            head[i].w = N;*/
        for( i = 0; i < m; i++)
        {
            int x, y , z;
            scanf("%d %d %d",&x,&y,&z);
            head[i].u = x;
            head[i].v = y;
            head[i].w = z;
        }
        kuaipai(0, m - 1);
        int q;
        scanf("%d",&q);
        for( i = 0; i < q; i++)
        {
            int t;
            scanf("%d",&t);
            printf("%d %d\n",head[t].u,head[t].v);
        }
    }
    return 0;
}

Published 177 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/Fusheng_Yizhao/article/details/104875195