sdutacm-图的基本存储的基本方式三

来源:互联网 发布:nginx 自定义404页面 编辑:程序博客网 时间:2024/05/18 00:20

图的基本存储的基本方式三

Time Limit: 1000MSMemory Limit: 65536KB

SubmitStatistic

ProblemDescription

解决图论问题,首先就要思考用什么样的方式存储图。但是小鑫却怎么也弄不明白如何存图才能有利于解决问题。你能帮他解决这个问题么?

Input

 多组输入,到文件结尾。

每一组第一行有两个数nm表示n个点,m条有向边。接下来有m行,每行两个数uvw代表uv有一条有向边权值为w。第m+2行有一个数q代表询问次数,接下来q行每行有一个询问,输入一个数为a

注意:点的编号为0~n-12<=n<=5000000<=m<=5000000<=q<=500000u!=vwint型数据。输入保证没有自环和重边

Output

 对于每一条询问,输出一行两个数xy。表示排序后第a条边是由xy的。对于每条边来说排序规则如下:

  1. 权值小的在前。
  2. 权值相等的边出发点编号小的在前
  3. 权值和出发点相等的到达点编号小的在前

注:边的编号自0开始

ExampleInput

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

ExampleOutput

1 3
0 1
1 2

Hint

 

Author

 lin

#include <stdio.h>#include <string.h>#include <stdlib.h>struct node{   int u,v,w;}p[500010],t;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 n, m,i, d,q;      while(scanf("%d%d",&n,&m)!=EOF)      {          for(i = 0;i<m;i++)          {            scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);          }          qsort(p,m,sizeof(node),cmp);          scanf("%d",&d);          while(d--)          {             scanf("%d",&q);             printf("%d %d\n",p[q].u,p[q].v);          }      }      return 0;   }/***************************************************User name: jk160505徐红博Result: AcceptedTake time: 120msTake Memory: 1536KBSubmit time: 2017-02-14 10:55:09****************************************************/


0 0