题意
思路:
模板题
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct Node{
int address,key,next;
bool flag;
}node[maxn];
bool cmp(Node a,Node b){
if(a.flag==false||b.flag==false){
return a.flag>b.flag;
}else{
return a.key<b.key;
}
}
int main(){
int n,begin,s,d,e,cnt=0;
cin >> n >> begin;
for(int i=0;i<maxn;i++){
node[i].flag=false;
}
for(int i=0;i<n;i++){
cin >> s >> d >> e;
node[s]={s,d,e,false};
}
for(int i=begin;i!=-1;i=node[i].next){
node[i].flag=true;
cnt++;
}
sort(node,node+maxn,cmp);
if(cnt==0) printf("0 -1");
else{
sort(node,node+maxn,cmp);
printf("%d %05d\n",cnt,node[0].address);
for(int i=0;i<cnt;i++){
printf("%05d %d",node[i].address,node[i].key);
if(i==cnt-1) printf(" -1");
else printf(" %05d\n",node[i+1].address);
}
}
return 0;
}
静态链表的模板:
1.定义静态链表:
struct Node{
int address,data,next;
XXX;
}node[100010];
2.初始化:
for(int i=0;i<maxn;i++){
node[i].XXX=0;
}
3.遍历整条链表
int p=begin,cnt=0;
while(p!=-1){
XXX=1;
cnt++;
p=node[p].next;
}
4.排序
bool cmp(Node a,Node b){
if(a.XXX==-1||b.XXX==-1){
return a.XXX>b.XXX;
}else{
二级排序;
}