首页 > 学院 > 开发设计 > 正文

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

2019-11-06 07:27:07
字体:
来源:转载
供稿:网友

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

Time Limit: 1000MSMemory Limit: 65536KB

SubmitStatistic

PRoblemDescription

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

Input

 多组输入,到文件结尾。

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

注意:点的编号为0~n-1,2<=n<=500000,0<=m<=500000,0<=q<=500000,u!=v,w为int型数据。输入保证没有自环和重边

Output

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

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

注:边的编号自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****************************************************/


上一篇:MapReduce

下一篇:二叉搜索树之前驱后继

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表