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

废话少说,原题链接: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. xposed XDA记录

    [OFFICIAL] Xposed for Lollipop/Marshmallow [Android 5.0/5.1/6.0, v86, 2016/10/31] http://forum.xda-d ...

  2. java 部分隐藏字段

    项目中,很多时候要对数据作隐私保护,隐藏一些数据的关键信息,比如身份证,隐藏其中的出生年月 //利用正则表达式匹配替换字符串 String email = "young452wer@gami ...

  3. css3 动画的有关属性

    transform transform: none|transform-functions; transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. ...

  4. Jsonp调用网易云音乐API搜索播放歌曲

    效果如下图: 基本就是正常的文件播放,暂停,停止,设置循环,随机播放,加速,减速,上一曲,下一曲,再多个选择本地文件加入到播放列表的功能.然后想着给加个能搜索网络歌曲并且播放的功能,今天研究了一下,成 ...

  5. CSS 中Font Awesome 图标(附码表)

    HTML中缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. Font Awesome 传送门:http://fontawesome.das ...

  6. 名词含义阅读 todolist

    1.node webkit 2.C#设计模式 3.算法导论 4.SQLSERVER RowNum() 5.图片文字识别 6.tuple 7.yield 8.Web语义化 (多用 p ul ol li ...

  7. 如何让include标签包裹的布局置于屏幕最下方?

    如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性  a ...

  8. 【Java EE 学习 67 下】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】

    一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar ...

  9. WM_COPYDATA实现的不同进程间通信

    进程间通信,通过SendMessage向另一进程发送WM_COPYDATA消息,实现不同进程间的消息通信. 需求:已写好一个工具软件,想在不更改当前的软件开发的前提下,实现为后面新开发的软件提供数据推 ...

  10. WCF 学习篇

    写在前面 自从运用了.NET Remoting 之后,就想系统的学习下WCF,因为WCF是对现有分布式通信技术的整合.主要以 <WCF全面解析> 这本书为主,园子的资料和网上资料为辅,来学 ...