Basic method for basic storage of graphs three

Source: Internet
Author: User
Tags first row
Basic method for basic storage of graphs three Time limit:1000ms Memory limit:65536k in doubt. Dot here ^_^ Title Description

To solve the problem of graph theory, we must first think about how to store the graph. But Xiao Xin but how also can not understand how to save a map to help solve the problem. Can you help him to solve the problem? Enter multiple sets of inputs to the end of the file.

The first row of each group has two numbers n, m represents n points, and m bars have a forward edge. Next there are m lines, two numbers per line, U, V, W for u to V with a forward Benquan value of W. Line M+2 has a number Q for the number of queries, followed by Q line each line has a query, enter a number of a

Note: The point number is 0~n-1,2<=n<=500000, and 0<=m<=500000,0<=q<=500000,u!=v,w is the int type data. Input guarantees no self-loop and heavy-side output for each query, the output line is two x, Y. Indicates that the first edge of a sort is x through Y. For each edge, the collation is as follows:

The weights are small in front.

The weight of the equal edge starting point number small in the preceding weight and starting point equal arrival point number small in the front

Note: The edges are numbered from 0 to start the sample input

4 3
0 1 1
1 2 2
1 3 0
3
0
1
2
Sample Output
1 3
0 1
1 2
Tips SourceLin Sample Program
#include <stdio.h>  
#include <stdlib.h>  
#define MAX 2147483647  
struct node  
{  
int u;  
int V;  
int w;  
} S[500010];  
int cmp (const void *a,const void *b)  
{  
struct node *c= (struct node *) A;  
struct node *d= (struct node *) b;  
if (c->w!=d->w)  
return c->w-d->w;  
else if (c->u!=d->u)  
return c->u-d->u;  
else  
return c->v-d->v;  
}  
int main ()  
{  
int i,j,n,m,k,t,l1,l2,l3;  
while (scanf ("%d%d", &n,&m)!=eof)  
{for  
(i=0;i<n;i++)  
S[i].w=max;  
for (i=0;i<m;i++)  
{  
scanf ("%d%d%d", &l1,&l2,&l3);  
S[I].U=L1;  
S[I].V=L2;  
s[i].w=l3;  
}  
Qsort (S,m,sizeof (s[0]), CMP);  
scanf ("%d", &k);  
while (k--)  
{  
scanf ("%d", &t);  
printf ("%d%d\n", S[T].U,S[T].V);}}}  
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.