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 ;
}
随机推荐
- apk逆向 - smali动态调试
author: Dlive date: 2016/10/6 0x00 前言 之前有人问过smali的动态调试方法,其实网上已经有很多文章讲这些内容,但是为了方便大家学习,我还是写一下让大家少走点坑 ...
- 第6章 Spring的事物处理
一.简述事物处理 1.事物处理的基本概念 1)提交:所有操作步骤都被完整执行后,称该事物被提交 2)回滚:某步操作执行失败,所有操作都没被提交,则事物必须被回滚 2.事物处理的特性(ACID) 1)原 ...
- tp框架之函数调用
1.如果要在一个方法里面调用另一个方法,可以先用A方法实例化控制器 $m = A("控制器名"); ,然后根据方法名调用 $m->方法名(可传参数); 2.自定义函数库 ( ...
- C++ 系列:socket 资料收集
Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- VisualSVN Server的配置和使用方法(转)
1.为什么要用VisualSVN Server,而不用Subversion? 回答: 因为如果直接使用Subversion,那么在Windows 系统上,要想让它随系统启动,就要封装SVN Serve ...
- Python 学习第十六天 html 前端内容总结
一,css知识总结 1, css属性 css的属性包括以下内容 position:规定元素的定位类型 background:属性在一个声明中设置所有的背景属性 可以设置的如下属性: (1)back ...
- Java EE之搭建论坛系统(使用JForum)
1.下载JForum开源压缩包: 网址:http://jforum.net/ (或者直接使用百度云中的压缩包) 解压,修改解压后文件下的\WEB-INF\config\database\mysql目 ...
- 使用极光推送(www.jpush.cn)向安卓手机推送消息【服务端向客户端主送推送】C#语言
在VisualStudio2010中新建网站JPushAndroid.添加引用json帮助类库Newtonsoft.Json.dll. 在web.config增加appkey和mastersecret ...
- 6种方法实现asp.net返回上一页
其实要实现返回上一页的功能,主要还是要用到JavaScript. 一: 在ASP.net的aspx里面的源代码中 <input type="button onclick="J ...
- js模拟抛出球运动
js练手之模拟水平抛球运动 -匀加速运动 -匀减速运动 模拟运动有些基本的思路,当前所在点的坐标,元素的长宽是多少,向右/向下运动x/y增加,向上/向左运动x/y减少,运动的路程是多少,用什么方程进行 ...