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:

  1. 9 12
  2. 0 1 6
  3. 0 2 4
  4. 0 3 5
  5. 1 4 1
  6. 2 4 1
  7. 3 5 2
  8. 5 4 0
  9. 4 6 9
  10. 4 7 7
  11. 5 7 4
  12. 6 8 2
  13. 7 8 4

Sample Output 1:

  1. 18

Sample Input 2:

  1. 4 5
  2. 0 1 1
  3. 0 2 2
  4. 2 1 3
  5. 1 3 4
  6. 3 2 5

Sample Output 2:

  1. Impossible

  2. 我的答案
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. #define ERROR -1
  6. #define false 0
  7. #define true 1
  8. #define MaxVertexNum 100
  9. #define INFINITY 65535
  10. #define MaxQueue 100
  11. typedef int Vertex;
  12. typedef int WeightType;
  13. typedef int bool;
  14.  
  15. //边
  16. typedef struct ENode *PtrToENode;
  17. struct ENode {
  18. Vertex V1, V2;
  19. WeightType Weight;
  20. };
  21. typedef PtrToENode Edge;
  22.  
  23. //邻接点
  24. typedef struct AdjVNode *PtrToAdjVNode;
  25. struct AdjVNode {
  26. Vertex AdjV; //下标
  27. WeightType Weight; //边权重
  28. PtrToAdjVNode Next; //指向下一个邻接点
  29. };
  30.  
  31. //顶点
  32. typedef struct VNode {
  33. PtrToAdjVNode FirstEdge; //边表头指针
  34. // DataType Data; //存顶点的户数据
  35. }AdjList[MaxVertexNum];
  36.  
  37. //图结点
  38. typedef struct GNode *PtrToGNode;
  39. struct GNode {
  40. int Nv;
  41. int Ne;
  42. AdjList G;
  43. };
  44. typedef PtrToGNode LGraph;
  45.  
  46. struct QNode {
  47. Vertex Data[MaxQueue];
  48. int rear;
  49. int front;
  50. };
  51. typedef struct QNode *Queue;
  52.  
  53. int IsEmptyQ(Queue PtrQ)
  54. {
  55. return (PtrQ->front == PtrQ->rear);
  56. }
  57.  
  58. void AddQ(Queue PtrQ, Vertex item)
  59. {
  60. if((PtrQ->rear+)%MaxQueue == PtrQ->front) {
  61. printf("Queue full");
  62. return;
  63. }
  64. PtrQ->rear = (PtrQ->rear+)%MaxQueue;
  65. PtrQ->Data[PtrQ->rear] = item;
  66. }
  67.  
  68. Vertex DeleteQ(Queue PtrQ)
  69. {
  70. if(PtrQ->front == PtrQ->rear) {
  71. printf("Queue Empty");
  72. return -;
  73. } else {
  74. PtrQ->front = (PtrQ->front+)%MaxQueue;
  75. return PtrQ->Data[PtrQ->front];
  76. }
  77. }
  78.  
  79. LGraph CreateGraph(int VertexNum)
  80. {
  81. Vertex V;
  82. LGraph Graph;
  83.  
  84. Graph = (LGraph)malloc(sizeof(struct GNode));
  85. Graph->Nv = VertexNum;
  86. Graph->Ne = ;
  87.  
  88. for(V=;V<Graph->Nv;V++)
  89. Graph->G[V].FirstEdge = NULL;
  90.  
  91. return Graph;
  92. }
  93.  
  94. void InsertEdge(LGraph Graph, Edge E)
  95. {
  96. PtrToAdjVNode NewNode;
  97.  
  98. //有向边
  99. NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
  100. NewNode->AdjV = E->V2;
  101. NewNode->Weight = E->Weight;
  102. //向V1插入V2
  103. NewNode->Next = Graph->G[E->V1].FirstEdge;
  104. Graph->G[E->V1].FirstEdge = NewNode;
  105. }
  106.  
  107. LGraph BuildGraph()
  108. {
  109. LGraph Graph;
  110. Edge E;
  111. int Nv, i;
  112.  
  113. scanf("%d", &Nv);
  114. Graph = CreateGraph(Nv);
  115.  
  116. scanf(" %d\n", &(Graph->Ne));
  117. if(Graph->Ne != ) {
  118. E = (Edge)malloc(sizeof(struct ENode));
  119. for(i=;i<Graph->Ne;i++) {
  120. scanf("%d %d %d\n", &E->V1, &E->V2, &E->Weight);
  121. InsertEdge(Graph, E);
  122. }
  123. }
  124.  
  125. return Graph;
  126. }
  127.  
  128. void PrintGraph(LGraph Graph)
  129. {
  130. Vertex V;
  131. PtrToAdjVNode W;
  132. for(V=;V<Graph->Nv;V++) {
  133. printf("%d:", V);
  134. for(W=Graph->G[V].FirstEdge;W;W=W->Next) {
  135. printf("[%3d %3d] ", W->AdjV, W->Weight);
  136. }
  137. printf("\n");
  138. }
  139. }
  140.  
  141. /* 邻接表存储 - 拓扑排序算法 */
  142. bool TopSort( LGraph Graph, Vertex TopOrder[], Vertex Earliest[])
  143. { /* 对Graph进行拓扑排序, TopOrder[]顺序存储排序后的顶点下标 */
  144. int Indegree[MaxVertexNum], cnt;
  145. Vertex V;
  146. PtrToAdjVNode W;
  147.  
  148. Queue Q = (Queue)malloc(sizeof(struct QNode)*( Graph->Nv ));
  149.  
  150. /* 初始化Indegree[] */
  151. for (V=; V<Graph->Nv; V++)
  152. Indegree[V] = ;
  153.  
  154. /* 遍历图,得到Indegree[] */
  155. for (V=; V<Graph->Nv; V++)
  156. for (W=Graph->G[V].FirstEdge; W; W=W->Next)
  157. Indegree[W->AdjV]++; /* 对有向边<V, W->AdjV>累计终点的入度 */
  158.  
  159. /* 将所有入度为0的顶点入列 */
  160. for (V=; V<Graph->Nv; V++)
  161. if ( Indegree[V]== ) {
  162. AddQ(Q, V);
  163. Earliest[V] = ; //起点为0
  164. }
  165.  
  166. /* 下面进入拓扑排序 */
  167. cnt = ;
  168. while( !IsEmptyQ(Q) ){
  169. V = DeleteQ(Q); /* 弹出一个入度为0的顶点 */
  170. TopOrder[cnt++] = V; /* 将之存为结果序列的下一个元素 */
  171. /* 对V的每个邻接点W->AdjV */
  172. for ( W=Graph->G[V].FirstEdge; W; W=W->Next )
  173. if ( --Indegree[W->AdjV] == ) {/* 若删除V使得W->AdjV入度为0 */
  174. AddQ(Q, W->AdjV); /* 则该顶点入列 */
  175. Earliest[W->AdjV] = Earliest[V] + W->Weight;
  176. // if((Earliest[V]+W->Weight)>Earliest[W->AdjV] && Earliest[W->AdjV])
  177. // Earliest[W->AdjV] = Earliest[V] + W->Weight;
  178. }
  179. } /* while结束*/
  180.  
  181. if ( cnt != Graph->Nv )
  182. return false; /* 说明图中有回路, 返回不成功标志 */
  183. else
  184. return true;
  185. }
  186.  
  187. int main()
  188. {
  189. LGraph Graph;
  190. WeightType Earliest[MaxVertexNum];
  191. Vertex TopOrder[MaxVertexNum],V;
  192. int ret;
  193.  
  194. Graph = BuildGraph();
  195. // PrintGraph(Graph);
  196. ret = TopSort(Graph, TopOrder, Earliest);
  197. if(ret == false) {
  198. printf("Impossible");
  199. } else if(ret == true) {
  200. int max = Earliest[];
  201. for(int i=;i<Graph->Nv;i++) {
  202. // printf("%d: [%d]\n", i, Earliest[i]);
  203. if(max < Earliest[i])
  204. max = Earliest[i];
  205. }
  206. printf("%d", max);
  207. }
  208. // printf("TopOrder:");
  209. // for(V=0;V<Graph->Nv;V++)
  210. // printf("%d ", TopOrder[V]);
  211. // printf("\n");
  212. return ;
  213. }

08-图8 How Long Does It Take(25 分)邻接表和队列的更多相关文章

  1. 图的bfs遍历模板(邻接矩阵存储和邻接表存储)

    bfs遍历图模板伪代码: bfs(u){ //遍历u所在的连通块 queue q; //将u入队 inq[u] = true; while (q非空){ //取出q的队首元素u进行访问 for (从u ...

  2. 图结构练习——判断给定图是否存在合法拓扑序列(dfs算法(第一个代码),邻接矩阵(前两个代码),邻接表(第三个代码))

    sdut 2140 图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  给定一个有向图 ...

  3. 数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS

    图通常有两种表示方法: 邻接矩阵 和 邻接表 对于稀疏的图,邻接表表示能够极大地节省空间. 以下是图的数据结构的主要部分: struct Vertex{ ElementType element; // ...

  4. 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)

    数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...

  5. 图的邻接表存储表示(C)

    //---------图的邻接表存储表示------- #include<stdio.h> #include<stdlib.h> #define MAX_VERTEXT_NUM ...

  6. 数据结构(11) -- 邻接表存储图的DFS和BFS

    /////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...

  7. 邻接表存储图,DFS遍历图的java代码实现

    import java.util.*; public class Main{ static int MAX_VERTEXNUM = 100; static int [] visited = new i ...

  8. Invitation Cards(邻接表+逆向建图+SPFA)

    Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 17538   Accepted: 5721 Description In ...

  9. 图的建立——邻接表表示(C语言+VC6.0平台)

    图是一种重要而且相对复杂的数据结构,在实际编程中非常有用.邻接表是图的主要表示形式之一,是一种链接表表示方法. #include<stdio.h> #include<stdlib.h ...

  10. 图的邻接表存储 c实现

    图的邻接表存储 c实现 (转载) 用到的数据结构是 一个是顶点表,包括顶点和指向下一个邻接点的指针 一个是边表, 数据结构跟顶点不同,存储的是顶点的序号,和指向下一个的指针 刚开始的时候把顶点表初始化 ...

随机推荐

  1. 仅对原表新增列的全量数据.csv

    w

  2. 测开之路七十四:python处理kafka

    kafka-python地址:https://github.com/dpkp/kafka-python 安装kafka-python:pip install kafka-python 接收消息 fro ...

  3. vmware内部错误

    今天不知道怎么回事 wmware workstation重启的时候总是报内部错误 差点重装了 幸亏找到了这个 原来只要以管理员的身份运行vwware就ok了 http://www.xiaoluobok ...

  4. 排序算法二:归并排序(Merge sort)

    归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn). (一)算法实现 private void merge_sort(int[] array, int ...

  5. Waiter.js

    var Waiter = function() {     var dfd = [],         doneArr = [],         failArr = [],         slic ...

  6. zabbix 微信告警配置

    作者信息 邮箱:sijiayong000@163.com Q Q:601566386 Zabbix 微信告警 摘要:Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是 ...

  7. PowerShell 远程执行命令

    PowerShell 远程执行命令 最近在做一些自动化的测试工作,在代码实现的过程中需要远程启动/关闭一些服务或者测试机. 我首先想到的是建立一个website,通过网站对一些服务进行操作,但是这样感 ...

  8. Socket编程半双工

    服务器 package com.test; import java.io.IOException; import java.net.*; import java.io.*; public class ...

  9. HNOI2019fish

    \({\rm fish}\) 20分: 六个for,点积判锐角钝角. #include <vector> #include <queue> #include <cmath ...

  10. BZOJ 1683.City skyline 城市地平线

    传送门 从左到右扫一遍,考虑什么时候会和之前形成同一幢房子从而不用统计 显然是当前的高度和之前某个点高度相同,并且它们之间没有更矮的建筑 考虑用一个单调栈维护一个单调上升的房子轮廓,然后对于扫到的每一 ...