How Long Does It Take
好长时间没写博客了,真心惭愧啊!
废话少说,原题链接:https://pta.patest.cn/pta/test/1342/exam/4/question/24939
题目如下:
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 NNN (≤100\le 100≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N−1N-1N−1), and MMM, the number of activities. Then MMM 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
这道题不难,本质上就是一道拓扑排序的问题,只不过增加了权重,成为了一道关键路径的问题,此类拓扑排序问题的核心思想就是先在图中找到入度为0的点,对其进行操作,然后将于该点相邻的边删除,继续对剩下的图重复这一过程。为了提高算法的效率,陈越老师讲过可以在每次删除边时,检查其它顶点是否入度为0,如果为0就将其放在一个容器里面(我用的是队列),下次用的时候就可以直接从容器里面取出。拓扑排序一般是较为稀疏的图,应该用邻接表存储图合适,但我为了写起来简单,就用了邻接矩阵。以下是我的代码:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define Max 105
#define INFINITY 65535
typedef struct QNode{
int front,rear;
int Data[Max];
int Maxsize;
}Quenue; Quenue *CreateQ(int N)
{
Quenue *Q=(Quenue *)malloc(sizeof(struct QNode));
Q->front=Q->rear=;
Q->Maxsize=N+;
return Q;
} bool IsFull(Quenue *Q)
{
return ((Q->front+)%Q->Maxsize == Q->rear);
} void Push(Quenue *Q,int v)
{
if (!IsFull(Q))
{
Q->front=(Q->front+)%Q->Maxsize;
Q->Data[Q->front]=v;
}
} bool IsEmpty(Quenue *Q)
{
return (Q->front==Q->rear);
} int Pop(Quenue *Q)
{
if (!IsEmpty(Q))
{
Q->rear=(Q->rear+)%Q->Maxsize;
return (Q->Data[Q->rear]);
}
} int G[Max][Max];
int N,M; void TopSort()
{
int Indegree[Max]={};
int v,w,weight,cnt=;
int endcnt=; //有多个终点是,用来记录是否为终点的变量
int dist[Max]={};
Quenue *Q=CreateQ(N);
/* 初始化各顶点的入度值 */
for (v=;v<N;v++)
{
for (w=;w<N;w++)
{
if ( G[w][v]<INFINITY)Indegree[v]++;
}
}
/*入度为0的顶点入队 */
for (v=;v<N;v++)
{
if (Indegree[v]==){Push(Q,v);}
}
/*拓扑排序*/
weight=;
while (!IsEmpty(Q))
{
v=Pop(Q);
cnt++;
for (w=;w<N;w++)
{
if (G[v][w]<INFINITY)
{
if (dist[v]+G[v][w]>dist[w])dist[w]=dist[v]+G[v][w];
if (--Indegree[w]==)Push(Q,w);
endcnt++;
}
}
if (endcnt==) //此时v为终点
{
if (dist[v]>weight)weight=dist[v];
}
endcnt=;
}
if (cnt!=N)printf("Impossible");
else printf("%d",weight);
return ;
} int main()
{
scanf("%d %d",&N, &M);
int i,j,v,w,weight;
for (i=;i<N;i++){
for (j=;j<N;j++){G[i][j]=INFINITY;}}
for (i=;i<M;i++)
{
scanf("%d %d %d",&v, &w, &weight);
G[v][w]=weight;
}
TopSort();
return ;
}
随机推荐
- SQL中循环和条件语句
.if语句使用示例: declare @a int begin print @a end else begin print 'no' end .while语句使用示例: declare @i int ...
- SQL 查看数据库的列数
查询表名为History的所有列名 1 select name from syscolumns where id=object_id('History') 查询表名为History的所有列名个数 ...
- java.io.Serializable 序列化接口
什么是序列化.反序列化? Serialization(序列化)是一种将对象以一连串的字节描述的过程: 反序列化deserialization是一种将这些字节重建成一个对象的过程. 序列化通俗一点说就是 ...
- 完全卸载AndroidStudio
一:卸载Android Studio 由于从1.5正式版直接升级到2.1的版本,整个项目构建都变得异常的慢,所以决定卸载重新安装2.0的正式版.但是Mac下使用dmg安装的app很多都是不能使用拖拽的 ...
- zookeeper3.3.6 伪分布式安装
下载地址(http://zookeeper.apache.org/releases.html#download) 一:下载zookeeper的安装包,解压,进入到zk的目录文件,进入conf目录 ...
- dbca建库sys用户被锁
奇怪问题:dbca建库sys用户被锁, 点击密码管理报账户被锁 而且在服务器上无法进行操作系统验证登陆,经过一番检查发现oracle用户和grid用户没有在dba组里 解决: 1.把oracle用户和 ...
- Java并发集合的实现原理
本文简要介绍Java并发编程方面常用的类和集合,并介绍下其实现原理. AtomicInteger 可以用原子方式更新int值.类 AtomicBoolean.AtomicInteger.AtomicL ...
- js 倒计时
/** * 启动,秒杀倒计时. time格式:DDHH24MISS 例如: time="11223344"; * */function timer(time) { var d = ...
- F#之旅2 - 我有特别的学F#技巧
原文地址:https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/learning-fsharp/ Learning F#Functio ...
- UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(二)
上篇UWP VirtualizedVariableSizedGridView 支持可虚拟化可变大小Item的View(一) 讲到该控件的需要和设计过程. 这篇讲讲开发过程中一些重要问题解决. 1.支持 ...