好长时间没写博客了,真心惭愧啊!

废话少说,原题链接: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 ;
}

随机推荐

  1. 3ds max录制自定义视频

    要将视频设置成未压缩才能录制出自己设定的大小,其他都是都是特定的,软件默认的一些参数.

  2. Android中surface,surfaceview,sufaceholder以及surface客户端的关系

    这里以照相机camera功能的实现来解释surface,surfaceview,sufaceholder以及surface客户端(本例子中指的是camera)的关系,surface及其client(客 ...

  3. 【CentOS】LAMP相关4

    MySQL不支持TAB补全.mysql_history命令历史 用SOCKET形式登陆:mysql -uroot -p123456,mysql -uroot -p123456 -S /var/lib/ ...

  4. hellocharts折线图与柱状图的上下结合酷炫效果(学习笔记)

    二话不说先贴图 贴代码: LineColumnDependencyActivity.java package com.shaoxin.mylinecolumndependencyactivity; i ...

  5. 关于利用bat文件调用exe批量处理文件下的文件的问题

    for %%i in (E:\radar_20120721\sjz_sa\*.bin) do start/wait radas.exe -i=%%i -o=E:\longjiang\out 找到 文件 ...

  6. 餐厅点餐系统app总结

    总结: 三个冲刺已经结束,虽然没有说十分完美,但该实现的功能还是实现了,只是在市场是相较于专业性的缺乏竞争力,从界面到体验都需进一步优化. 每个人的进度不一样,为了同一个任务需要不断的磨合与合作,但慢 ...

  7. DFS序+线段树 hihoCoder 1381 Little Y's Tree(树的连通块的直径和)

    题目链接 #1381 : Little Y's Tree 时间限制:24000ms 单点时限:4000ms 内存限制:512MB 描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每 ...

  8. java-并发-保护代码块

    浏览以下内容前,请点击并阅读 声明 线程经常需要协调其动作,最常用的协调方法就是保护代码块,该代码块以一个条件判断开始,当判断为true时才能开始执行. 假设一个方法guradedJoy必须等到变量j ...

  9. 修改radio与check样式

    一般的radio与check的样式很难看,这个时候就需要我们自己修改其样式 逻辑思维: 1.用label包裹input标签以及样式标签,然后将radio定位到界面以外,设置样式标签的样式 2.使用伪类 ...

  10. unity 协程

    StartCoroutine在unity3d的帮助中叫做协程,意思就是启动一个辅助的线程. 在C#中直接有Thread这个线程,但是在unity中有些元素是不能操作的.这个时候可以使用协程来完成. 使 ...