#include
#include studio.h
为什么会这样,该怎样修改呢
注释如下
#include<iostream>
#include <string.h>
#include<vector>
#include<stdlib.h>
using namespace std;
struct node {
int t; //起始顶点
int w; //终点
int y; //权重
};
struct node a[20010]; //保存所有的边
int s[1010] = { 0 }; //保存每个顶点的作为起点的边的个数
int cmp(const void* a, const void* b) { //比较函数,用于排序
struct node* c = (node*)a; //void指针转为node指针,便于进行比较
struct node* d = (node*)b; //void指针转为node指针,便于进行比较
if (c->t != d->t) return c->t - d->t; //先根据边的起点比较
else return c->w - d->w; //如果起点相等,则根据终点进行比较
}
int main() {
int n, e, t, w, y, sum;
cin >> n >> e; //输入顶点和边的个数
for (int i = 0; i < e; i++) //初始化边结构体数组,这个for循环可以不要
{
a[i].t = -1; //初始化为-1
a[i].w = -1;
a[i].y = -1;
}
int max = 0; //这个变量没用
for (int i = 0; i < e; i++) { //输入e条边的信息
cin >> a[i].t >> a[i].w >> a[i].y; //输入起点、终点和权重
s[a[i].t]++; //t是起始顶点,s保存t作为起点时边的个数
}
qsort(a, e, sizeof(a[0]), cmp); //调用qsort进行排序,排序的比较函数逻辑由cmp提供(先根据边的起点排序,如果起点相同,再根据终点排序),从小到大排序
//输出
int i = 0;
while (i < e) { //输出e条边
sum = s[a[i].t]; //得到每个顶点作为起点的边的个数
printf("%d:", a[i].t); //输出起点id
for (int j = i; j < sum + i; j++) { //输出a[i].t作为起始顶点的所有边的信息
printf("(%d,%d,%d)", a[j].t, a[j].w, a[j].y); //输出起点、终点、权重
}
i = i + sum; //i是已经显示的边的个数,sum是每个顶点的边的个数,这里进行累加
printf("\n");
}
return 0;
}
struct node {//结构体定义
int t;//端点编号
int w;//端点编号
int y;//权值
};
struct node a[20010];//结构体数组
int s[1010] = { 0 };//整型数组
int cmp(const void* a, const void* b) {
struct node* c = (node*)a;
struct node* d = (node*)b;
if (c->t != d->t) return c->t - d->t;
else return c->w - d->w;
}
int n, e, t, w, y, sum;
cin >> n >> e;//键盘输入顶点数和边数
for (int i = 0; i < e; i++)//给每条边的端点编号和权值初始化为-1
{
a[i].t = -1;
a[i].w = -1;
a[i].y = -1;
}
int max = 0;
for (int i = 0; i < e; i++) {
cin >> a[i].t >> a[i].w >> a[i].y;//键盘输入每条边的端点编号和权值
s[a[i].t]++;//同一端点引出的边数累加
}
//快排函数
//a是一个地址,即参与排序的首地址
//e是需要排序的数量
//sizeof(a[0])则是每一个元素占用的空间大小
//cmp用于确定排序的顺序。
qsort(a, e, sizeof(a[0]), cmp);
//输出
int i = 0;
while (i < e) {
sum = s[a[i].t];//同一个端点引出的边数
printf("%d:", a[i].t);//输出端点编号
for (int j = i; j < sum + i; j++) {
printf("(%d,%d,%d)", a[j].t, a[j].w, a[j].y);//输出边
}
i = i + sum;
printf("\n");
}
return 0;
要vector的头文件吗?没有用vector喔?!