当前位置: 首页 > news >正文

08-图8 How Long Does It Take(C)

哈哈,很明显这是一个有向无环图,用邻接表更好一些 ,

这一个考察的是拓扑排序的简单应用。

测试点提示内存(KB)用时(ms)结果得分
0sample 1 一般情况,有0边,单个起点和单个终点1924

答案正确

15 / 15
1sample 2 有环3165

答案正确

2 / 2
2多个起点和多个终点3204

答案正确

4 / 4
3最大N,不可行3204

答案正确

2 / 2
4最大N,可行3164

答案正确

2 / 2

Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project.

Input Specification:

Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N−1), and M, the number of activities. Then M lines follow, each gives the description of an activity. For the i-th activity, three non-negative numbers are given: S[i], E[i], and L[i], where S[i] is the index of the starting check point, E[i] of the ending check point, and L[i] the lasting time of the activity. The numbers in a line are separated by a space.

Output Specification:

For each test case, if the scheduling is possible, print in a line its earliest completion time; or simply output "Impossible".

Sample Input 1:

9 12
0 1 6
0 2 4
0 3 5
1 4 1
2 4 1
3 5 2
5 4 0
4 6 9
4 7 7
5 7 4
6 8 2
7 8 4

Sample Output 1:

18

Sample Input 2:

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

Sample Output 2:

Impossible

 我的AC:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>typedef struct ENode *Edge;
struct ENode{int V1, V2;int Weight;
};typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{int V;int Weight;PtrToAdjVNode Next;
};typedef struct VNode *AdjList;
struct VNode{PtrToAdjVNode FirstEdge;
};typedef struct GNode *LGraph;
struct GNode{int Nv;int Ne;AdjList	*G;
};typedef struct Node *QNode;
struct Node{int data;QNode Next;
};typedef struct QNode *Queue;
struct QNode{QNode Front;QNode Rear;
};void Build_Graph(LGraph M, int *InDegree);
LGraph Init_Graph();
void Insert_Graph(LGraph M, Edge E);
Queue Init_Queue();
void Add_Q(Queue Q, int data);
int Delete_Q(Queue Q);
bool IsEmpty_Q(Queue Q);
bool TopSort(LGraph M, int *InDegree, int *EarthTime);
int Max_time(int *EarthTime, int N);int main()
{LGraph M;int *InDegree;int *EarthTime;M = Init_Graph();	EarthTime = (int*)calloc(M ->Nv, sizeof(int));InDegree = (int*)calloc(M ->Nv, sizeof(int));Build_Graph(M, InDegree);if(TopSort(M, InDegree, EarthTime)){printf("%d\n", Max_time(EarthTime, M ->Nv));}else{printf("Impossible\n");}return 0;
}
bool TopSort(LGraph M, int *InDegree, int *EarthTime)
{PtrToAdjVNode G;Queue Q;int V = 0;int num = 0;Q = Init_Queue();for(V = 0; V < M ->Nv; V++){if(!InDegree[V]){Add_Q(Q, V);}}while(!IsEmpty_Q(Q)){V = Delete_Q(Q);num++;for(G = M ->G[V] ->FirstEdge; G; G = G ->Next){if(--InDegree[G ->V] == 0){Add_Q(Q, G ->V);}if(EarthTime[G ->V] < EarthTime[V] + G ->Weight){EarthTime[G ->V] = EarthTime[V] + G ->Weight;
//				printf("EarthTime[%d] = %d (%d ->%d)\n", G ->V, EarthTime[G ->V], G ->V, G ->V);}}	}if(num != M ->Nv){return false;}else{return true;}
}
int Max_time(int *EarthTime, int N)
{int time = 0;for(int i = 0; i < N; i++){if(EarthTime[i] > time){time = EarthTime[i];}}return time;
}
void Build_Graph(LGraph M, int *InDegree)
{Edge E;int i;E = (Edge)malloc(sizeof(struct ENode));for(i = 0; i < M ->Ne; i++){scanf("%d %d %d", &E ->V1, &E ->V2, &E ->Weight);Insert_Graph(M, E);InDegree[E ->V2]++;}
}
LGraph Init_Graph()
{LGraph M;M = (LGraph)malloc(sizeof(struct GNode));scanf("%d %d", &M ->Nv, &M ->Ne);M ->G = (AdjList*)malloc(sizeof(AdjList) * M ->Nv);for(int i = 0; i < M ->Nv; i++){M ->G[i] = (AdjList)malloc(sizeof(struct VNode));M ->G[i] ->FirstEdge= NULL;}return M;
}
void Insert_Graph(LGraph M, Edge E)
{PtrToAdjVNode NewNode;NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));NewNode ->V = E ->V2;NewNode ->Weight = E ->Weight;NewNode ->Next = M ->G[E ->V1] ->FirstEdge;M ->G[E ->V1] ->FirstEdge = NewNode;}
Queue Init_Queue()
{Queue Q;Q = (Queue)malloc(sizeof(struct QNode));Q ->Front = NULL;Q ->Rear  = NULL;return Q;
}
void Add_Q(Queue Q, int Vertex)
{QNode Node;Node = (QNode)malloc(sizeof(struct Node));Node ->data = Vertex;Node->Next = NULL;if(!(Q ->Front) && !(Q ->Rear)){Q ->Front = Node;Q ->Rear = Node;}else{Q ->Rear ->Next = Node;Q ->Rear = Node;}
}
int Delete_Q(Queue Q)
{if(IsEmpty_Q(Q)){printf("很遗憾,是空的!\n");return -1;}else{QNode Temp;int Vertex;Temp = Q ->Front;Q ->Front = Temp ->Next;Vertex = Temp ->data;if(IsEmpty_Q(Q)){Q ->Rear = NULL;}else{free(Temp);}return Vertex;}
}
bool IsEmpty_Q(Queue Q)
{return (Q ->Front == NULL);
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 移动应用门户实现的技术方案
  • 用了虚拟机后,本机摄像头打不开了(联想电脑thinkpad)
  • [数据集][目标检测]血细胞检测数据集VOC+YOLO格式2757张4类别
  • 2024国赛数学建模B题完整分析参考论文38页(含模型和可运行代码)
  • 家里有猫用宠物空气净化器有用吗?希喂、米家、有哈哪款更好
  • 在springboot中如何使用Jetty替换Tomcat
  • 同样数据源走RTMP播放延迟低还是RTSP低?
  • Redis的设计哲学和实现方式
  • Maven创建项目中的groupId, artifactId, 和 version的意思
  • Docker 的安装和使用
  • Day-04-QFile打开文件的两种方式
  • UNIX IPC方法的分类
  • 进程+线程+协程
  • Rust的常数、作用域与所有权
  • 如何将图表数据拟合为函数
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • 【EOS】Cleos基础
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • CSS魔法堂:Absolute Positioning就这个样
  • CSS实用技巧干货
  • ES10 特性的完整指南
  • GitUp, 你不可错过的秀外慧中的git工具
  • Java 23种设计模式 之单例模式 7种实现方式
  • vue-router的history模式发布配置
  • webpack入门学习手记(二)
  • 前端面试之闭包
  • 前端面试总结(at, md)
  • 入手阿里云新服务器的部署NODE
  • 设计模式(12)迭代器模式(讲解+应用)
  • 移动端解决方案学习记录
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • ​【数据结构与算法】冒泡排序:简单易懂的排序算法解析
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • ​什么是bug?bug的源头在哪里?
  • #define MODIFY_REG(REG, CLEARMASK, SETMASK)
  • #if 1...#endif
  • #中的引用型是什么意识_Java中四种引用有什么区别以及应用场景
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (搬运以学习)flask 上下文的实现
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (第三期)书生大模型实战营——InternVL(冷笑话大师)部署微调实践
  • (二)WCF的Binding模型
  • (二十九)STL map容器(映射)与STL pair容器(值对)
  • (二十三)Flask之高频面试点
  • (三)c52学习之旅-点亮LED灯
  • (四)js前端开发中设计模式之工厂方法模式
  • (转)详解PHP处理密码的几种方式
  • *算法训练(leetcode)第四十五天 | 101. 孤岛的总面积、102. 沉没孤岛、103. 水流问题、104. 建造最大岛屿
  • .Net Attribute详解(上)-Attribute本质以及一个简单示例
  • .NET Core中的去虚
  • .Net MVC4 上传大文件,并保存表单
  • .NET 项目中发送电子邮件异步处理和错误机制的解决方案