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

我的答案
 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #define ERROR -1
#define false 0
#define true 1
#define MaxVertexNum 100
#define INFINITY 65535
#define MaxQueue 100
typedef int Vertex;
typedef int WeightType;
typedef int bool; //边
typedef struct ENode *PtrToENode;
struct ENode {
Vertex V1, V2;
WeightType Weight;
};
typedef PtrToENode Edge; //邻接点
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode {
Vertex AdjV; //下标
WeightType Weight; //边权重
PtrToAdjVNode Next; //指向下一个邻接点
}; //顶点
typedef struct VNode {
PtrToAdjVNode FirstEdge; //边表头指针
// DataType Data; //存顶点的户数据
}AdjList[MaxVertexNum]; //图结点
typedef struct GNode *PtrToGNode;
struct GNode {
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph; struct QNode {
Vertex Data[MaxQueue];
int rear;
int front;
};
typedef struct QNode *Queue; int IsEmptyQ(Queue PtrQ)
{
return (PtrQ->front == PtrQ->rear);
} void AddQ(Queue PtrQ, Vertex item)
{
if((PtrQ->rear+)%MaxQueue == PtrQ->front) {
printf("Queue full");
return;
}
PtrQ->rear = (PtrQ->rear+)%MaxQueue;
PtrQ->Data[PtrQ->rear] = item;
} Vertex DeleteQ(Queue PtrQ)
{
if(PtrQ->front == PtrQ->rear) {
printf("Queue Empty");
return -;
} else {
PtrQ->front = (PtrQ->front+)%MaxQueue;
return PtrQ->Data[PtrQ->front];
}
} LGraph CreateGraph(int VertexNum)
{
Vertex V;
LGraph Graph; Graph = (LGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = ; for(V=;V<Graph->Nv;V++)
Graph->G[V].FirstEdge = NULL; return Graph;
} void InsertEdge(LGraph Graph, Edge E)
{
PtrToAdjVNode NewNode; //有向边
NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->AdjV = E->V2;
NewNode->Weight = E->Weight;
//向V1插入V2
NewNode->Next = Graph->G[E->V1].FirstEdge;
Graph->G[E->V1].FirstEdge = NewNode;
} LGraph BuildGraph()
{
LGraph Graph;
Edge E;
int Nv, i; scanf("%d", &Nv);
Graph = CreateGraph(Nv); scanf(" %d\n", &(Graph->Ne));
if(Graph->Ne != ) {
E = (Edge)malloc(sizeof(struct ENode));
for(i=;i<Graph->Ne;i++) {
scanf("%d %d %d\n", &E->V1, &E->V2, &E->Weight);
InsertEdge(Graph, E);
}
} return Graph;
} void PrintGraph(LGraph Graph)
{
Vertex V;
PtrToAdjVNode W;
for(V=;V<Graph->Nv;V++) {
printf("%d:", V);
for(W=Graph->G[V].FirstEdge;W;W=W->Next) {
printf("[%3d %3d] ", W->AdjV, W->Weight);
}
printf("\n");
}
} /* 邻接表存储 - 拓扑排序算法 */
bool TopSort( LGraph Graph, Vertex TopOrder[], Vertex Earliest[])
{ /* 对Graph进行拓扑排序, TopOrder[]顺序存储排序后的顶点下标 */
int Indegree[MaxVertexNum], cnt;
Vertex V;
PtrToAdjVNode W; Queue Q = (Queue)malloc(sizeof(struct QNode)*( Graph->Nv )); /* 初始化Indegree[] */
for (V=; V<Graph->Nv; V++)
Indegree[V] = ; /* 遍历图,得到Indegree[] */
for (V=; V<Graph->Nv; V++)
for (W=Graph->G[V].FirstEdge; W; W=W->Next)
Indegree[W->AdjV]++; /* 对有向边<V, W->AdjV>累计终点的入度 */ /* 将所有入度为0的顶点入列 */
for (V=; V<Graph->Nv; V++)
if ( Indegree[V]== ) {
AddQ(Q, V);
Earliest[V] = ; //起点为0
} /* 下面进入拓扑排序 */
cnt = ;
while( !IsEmptyQ(Q) ){
V = DeleteQ(Q); /* 弹出一个入度为0的顶点 */
TopOrder[cnt++] = V; /* 将之存为结果序列的下一个元素 */
/* 对V的每个邻接点W->AdjV */
for ( W=Graph->G[V].FirstEdge; W; W=W->Next )
if ( --Indegree[W->AdjV] == ) {/* 若删除V使得W->AdjV入度为0 */
AddQ(Q, W->AdjV); /* 则该顶点入列 */
Earliest[W->AdjV] = Earliest[V] + W->Weight;
// if((Earliest[V]+W->Weight)>Earliest[W->AdjV] && Earliest[W->AdjV])
// Earliest[W->AdjV] = Earliest[V] + W->Weight;
}
} /* while结束*/ if ( cnt != Graph->Nv )
return false; /* 说明图中有回路, 返回不成功标志 */
else
return true;
} int main()
{
LGraph Graph;
WeightType Earliest[MaxVertexNum];
Vertex TopOrder[MaxVertexNum],V;
int ret; Graph = BuildGraph();
// PrintGraph(Graph);
ret = TopSort(Graph, TopOrder, Earliest);
if(ret == false) {
printf("Impossible");
} else if(ret == true) {
int max = Earliest[];
for(int i=;i<Graph->Nv;i++) {
// printf("%d: [%d]\n", i, Earliest[i]);
if(max < Earliest[i])
max = Earliest[i];
}
printf("%d", max);
}
// printf("TopOrder:");
// for(V=0;V<Graph->Nv;V++)
// printf("%d ", TopOrder[V]);
// printf("\n");
return ;
}

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. p5471 [NOI2019]弹跳

    分析 代码 #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define ...

  2. P2239螺旋矩阵

    传送 看到这数据范围,显然咱不能暴力直接模拟(二维数组开不下,而且会T掉) 我们目前有两种选择: 1.优化暴力  走这边(jyy tql%%%) 2.数学做法 我们看一下题目中的那个矩阵 我们能不能找 ...

  3. 大数据学习笔记之Zookeeper(二):Zookeeper实战篇(一)

    文章目录 2.1 本地模式安装部署 2.2 配置参数解读 2.1 本地模式安装部署 1)安装前准备: (1)安装jdk (2)通过filezilla工具拷贝zookeeper到到linux系统下 (3 ...

  4. CSAPP:局部性原理

    一个编写良好的计算机程序常常具有良好的局部性(locality).局部性通常有两种不同的形式:时间局部性(temporal locality)和空间局部性(spatial locality).在一个具 ...

  5. 搭建用例管理平台phpstudy+testlink代替Apache+MySQL+PHP环境,以及testlink搭建环境报错修复

    公司需要搭建一个用例管理平台,环境搭建Apache+MySQL+PHP环境 哇一看就是需要花很长时间去搭建环境,本来我也在用这样的笨方法,不小心被公司开发看到,经人家一提点,哎呀妈呀发现自己以前的方法 ...

  6. spring-第十九篇AOP面向切面编程之增强处理的优先级

    1.从我们第十七篇举例了不同类型的增强处理. spring AOP采用和AspectJ一样的优先顺序来织入增强处理:在“进入”连接点时,具有最高优先级的增强处理将先被织入(在给定的两个Before增强 ...

  7. Python模块之pdfkit

    1.安装依赖 pip install python-docx #Python下的Microsoft Word 2007工具 pip install PyPDF2 #Python下的PDF工具 pip ...

  8. 7、numpy——广播

    1.广播的引出 广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 如果两个数组 a 和 b 形状相同,即满足 a. ...

  9. POJ 1438 One-way Traffic (混合图+边双连通)

    <题目链接> 题目大意: 给定一个混合图,问你在能够使得图中所有点能够两两到达的情况下,尽可能多的将无向边变成有向边,输出这些无向边的变化方案. 解题分析:这与之前做过的这道题非常类似 P ...

  10. sql server 自优化

    大数据量下的SQL Server数据库自身优化 发布时间:2013-12-17 15:19:00 来源:论坛 作者:佚名   关键字:数据库开发 1.1:增加次数据文件 从SQL SERVER 200 ...