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

来源:互联网 发布:直播软件xsplit 编辑:程序博客网 时间:2024/04/30 08:21

Problem Description

解决图论问题,首先就要思考用什么样的方式存储图。但是小鑫却怎么也弄不明白如何存图才能有利于解决问题。你能帮他解决这个问题么?
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的。对于每条边来说排序规则如下:

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

注:边的编号自0开始
Example Input

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

Example Output

1 3
0 1
1 2


排序有点乱的说..

#include <stdio.h>#include <iostream>using namespace std;struct node{    int u,v,w;}head[500005];void qsort(int left,int right){    int i=left,j=right;    if(left>=right)        return ;    int t1=head[left].u,t2=head[left].v,t3=head[left].w;    while(i<j)    {        while(i<j&&((head[j].w>t3)||(head[j].w==t3&&head[j].u>t1)||(head[j].w==t3&&head[j].u==t1&&head[j].v>t2)))            j--;        head[i]=head[j];        while(i<j&&((head[i].w<t3)||(head[i].w==t3&&head[i].u<t1)||(head[i].w==t3&&head[i].u==t1&&head[i].v<t2)))            i++;        head[j]=head[i];    }    head[i].u=t1;    head[i].v=t2;    head[i].w=t3;    qsort(left,i-1);    qsort(i+1,right);}int main(){    int n,m,x,y,a;    while(cin>>n>>m)    {        int i;        for(i=0;i<m;i++)        {            cin>>head[i].u>>head[i].v>>head[i].w;        }        int q;        cin>>q;        qsort(0,m-1);        while(q--)        {            cin>>a;            printf("%d %d\n",head[a].u,head[a].v);        }    }    return 0;}/***************************************************User name: Result: AcceptedTake time: 252msTake Memory: 548KBSubmit time: 2017-02-20 09:06:09****************************************************/
0 0
原创粉丝点击