PTA 08-图8 How Long Does It Take (25分)
题目地址
https://pta.patest.cn/pta/test/16/exam/4/question/674
5-12 How Long Does It Take (25分)
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 NN (\le 100≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N-1N−1), and MM, the number of activities. Then MM 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
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-05 14:42 答案正确 25 5-12 gcc 18 1
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 15/15 2 1
测试点2 答案正确 2/2 2 1
测试点3 答案正确 4/4 2 1
测试点4 答案正确 2/2 2 1
测试点5 答案正确 2/2 18 1 简单的拓扑排序,注意处理多起点和多终点的问题
*/
#include<stdio.h>
#define MAXN 100
#define TRUE 1
#define FALSE 0
#define ERROR -1
struct checkpoint{
int minStratTime;
int inEdgeCount;
int finished;
}gCheckpointTable[MAXN]; int gMatrix[MAXN][MAXN]; void InitCheckpointTable()
{
int i;
for(i=0;i<MAXN;i++)
{
gCheckpointTable[i].minStratTime=0;
gCheckpointTable[i].inEdgeCount=0;
gCheckpointTable[i].finished=0;
}
}
void InitMatrix(N)
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
gMatrix[i][j]=ERROR;
} int FindPoint(N) //返回一个入度为0并且没被访问过的点
{
int i;
int p=ERROR;
for(i=0;i<N;i++)
{
if(gCheckpointTable[i].inEdgeCount==FALSE && gCheckpointTable[i].finished==FALSE)
p=i;
}
return p;
} void Calc(int N)
{
int i,maxtime,p;
while( (p=FindPoint(N)) != ERROR ) //如果还能返回入度为0的点 先叫它v1
{
gCheckpointTable[p].finished=TRUE; //先设为处理完成
for(i=0;i<N;i++)//遍历该点所有发出去的边,找一个连接到的点v2
{
if(gMatrix[p][i]==ERROR)
continue;
//如果被指向的结点v2,最小完成时间小于此节点v1的最小完成时间 加上 v1到v2的耗时,那么更新v2的最小时间
if(gCheckpointTable[i].minStratTime<gCheckpointTable[p].minStratTime+gMatrix[p][i])
gCheckpointTable[i].minStratTime = gCheckpointTable[p].minStratTime + gMatrix[p][i];
gCheckpointTable[i].inEdgeCount--; //将v2的入度减一
}
}
maxtime=0;
for(i=0;i<N;i++) //考虑到有多终点问题,算完后把所有节点全扫一遍,挑一个结束时间最大的打出来
{
if(gCheckpointTable[i].finished==FALSE)
{
printf("Impossible");
return;
}
if(gCheckpointTable[i].minStratTime>maxtime)
maxtime=gCheckpointTable[i].minStratTime;
}
printf("%d",maxtime);
} int main()
{
int i;
int N,M;
int v1,v2,lastingTime;
scanf("%d %d",&N,&M);
InitMatrix(N);
InitCheckpointTable();
for(i=0;i<M;i++)
{
scanf("%d %d %d",&v1,&v2,&lastingTime);
gMatrix[v1][v2]=lastingTime;
gCheckpointTable[v2].inEdgeCount++;
}
Calc(N);
}
PTA 08-图8 How Long Does It Take (25分)的更多相关文章
- PTA 银行排队问题之单队列多窗口服务 (25分)
PTA 银行排队问题之单队列多窗口服务 (25分) 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选 ...
- PTA 10-排序6 Sort with Swap(0, i) (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i) (25分) Given a ...
- PTA 11-散列2 Hashing (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/679 5-17 Hashing (25分) The task of this pro ...
- PTA 09-排序1 排序 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/720 5-12 排序 (25分) 给定NN个(长整型范围内的)整数,要求输出从小到大 ...
- PTA L2-023 图着色问题-前向星建图 团体程序设计天梯赛-练习集
L2-023 图着色问题 (25 分) 图着色问题是一个著名的NP完全问题.给定无向图,,问可否用K种颜色为V中的每一个顶点分配一种颜色,使得不会有两个相邻顶点具有同一种颜色? 但本题并不是要你解 ...
- PTA 03-树1 树的同构 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构 (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...
- PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分)
PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...
- PTA 树的同构 (25分)
PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- PTA - - 06-图1 列出连通集 (25分)
06-图1 列出连通集 (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...
随机推荐
- SQL Server 2008添加字段成功,但提示列名无效
在sql后查询,给现有表添加一个字段,即执行sql语句: alter table [sxrq_1108].[dbo].[公司周报表详情] add 总计 int default 0 然后在上述sql查语 ...
- ios 自定义加载动画效果
在开发过程中,可能会遇到各种不同的场景需要等待加载成功后才能显示数据.以下是自定义的一个动画加载view效果. 在UIViewController的中加载等到效果,如下 - (void)vi ...
- 51nod 1089 最长回文子串 V2(Manacher算法)
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3. Input 第1行:一个数T,表示后面用作输入 ...
- JNI工程搭建及编译
JNI工程搭建及编译 建立Java工程 在具有C/C++比编译器的Eclipse中进行工程的创建,先创建一个简单的Java project,选项和一般同,这里仅仅需要将要调用的C/C++函数声明为na ...
- Codeforces Round #318 (Div. 2) D Bear and Blocks (数学)
不难发现在一次操作以后,hi=min(hi-1,hi-1,hi+1),迭代这个式子得到k次操作以后hi=min(hi-j-(k-j),hi-k,hi+j-(k-j)),j = 1,2,3... 当k ...
- PHP框架深度解析
PHP成为世界上最流行的脚本语言有许多原因:灵活性,易用性等等.但通常只用PHP或者其他语言编码就会显得单调.重复,这时候就需要一个PHP框架来代替程序员完成那些重复不变的部分.本文通过回答What, ...
- Spring框架中的aop操作之一 及aspectjweaver.jar与aopalliance-1.0.jar下载地址 包含beans 注解context 和aop的约束
(aspect oriented programming面向切面编程) 首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression ...
- iOS UI 设计
优设 http://www.uisdc.com Sketch http://www.sketchcn.com
- odoo10 fields.Selection 根据权限显示不同的selection内容
摘要:一般作为下拉选项,selection的选项内容是固定,针对一些特殊要求,根据权限组显示不同的selection内容的,可以参考odoo源码的. 前提:基于 odoo10.0 的源码 参考源码1: ...
- 对于WebAssembly编译出来的.wasm文件js如何调用
WebAssembly也叫浏览器字节码技术 这里就不过多的解释了网上很多介绍 主要是让大家知道在js里面如何调用执行它,我之前看WebAssemblyAPI时候反正是看得一脸懵逼 也是为了大家能更快的 ...