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

废话少说,原题链接: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. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  2. SQL(横表和纵表)行列转换,PIVOT与UNPIVOT的区别和使用方法举例,合并列的例子

    使用过SQL Server 2000的人都知道,要想实现行列转换,必须综合利用聚合函数和动态SQL,具体实现起来需要一定的技巧,而在SQL Server 2005中,使用新引进的关键字PIVOT/UN ...

  3. 解决 odoo.py: error: option --addons-path: The addons-path 'local-addons/' does not seem to a be a valid Addons Directory!

    情况说明 odoo源文件路径-/odoo-dev/odoo/: 我的模块插件路径 ~/odoo-dev/local-addons/my-module 在my-module中创建了__init__.py ...

  4. 读取Simulink中Dataset类型的数据

    http://files.cnblogs.com/files/pursuiting/%E5%80%92%E7%AB%8B%E6%91%86%E6%8E%A7%E5%88%B6%E7%B3%BB%E7% ...

  5. 浅谈Yii-admin的权限控制

    说到CMS,最需要有的东西就是权限控制,特别是一些复杂的场景,多用户,多角色,多部门,子父级查看等等.最近在开发一个线下销售的东东,这个系统分为管理员端,省代端,客户端,门店端,销售端, 部门端,部门 ...

  6. 转:SqlServer2012自增列值突然增大1000的原因及解决方法

    原文链接:http://blog.csdn.net/phoenix36999/article/details/53304126 首先排除数据回滚及增加删除等操作. 按照这篇文章SQL Server 2 ...

  7. 【学习篇:他山之石,把玉攻】jquery实现调用webservice

    1.webservice端 using System; using System.Collections.Generic; using System.Web; using System.Web.Ser ...

  8. CozyRSS开发记录18-番外之Atom1.0的支持

    CozyRSS开发记录18-番外之Atom1.0的支持 1.对CozyRSS.Syndication批判一番 由于我工作的主要开发语言是c++,所以会看到我的c#代码写得非常朴素,很多语法糖都没有用上 ...

  9. Android自定义组件

    [参考的原文地址] http://blog.csdn.net/l1028386804/article/details/47101387效果图: 实现方式: 一:自定义一个含有EditText和Butt ...

  10. ExecutorService中submit()和execute()的区别

    在使用java.util.concurrent下关于线程池一些类的时候,相信很多人和我一样,总是分不清submit()和execute()的区别,今天从源码方面分析总结一下. 通常,我们通过Execu ...