一个项目由若干个任务组成,任务之间有先后依赖顺序。项目经理需要设置一系列里程碑,在每个里程碑节点处检查任务的完成情况,并启动后续的任务。现给定一个项目中各个任务之间的关系,请你计算出这个项目的最早完工时间。

输入格式:

首先第一行给出两个正整数:项目里程碑的数量 N(≤100)和任务总数 M。这里的里程碑从 0 到 N−1 编号。随后 M 行,每行给出一项任务的描述,格式为“任务起始里程碑 任务结束里程碑 工作时长”,三个数字均为非负整数,以空格分隔。

输出格式:

如果整个项目的安排是合理可行的,在一行中输出最早完工时间;否则输出"Impossible"。

输入样例 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

输出样例 1:

18

输入样例 2:

4 5
0 1 1
0 2 2
2 1 3
1 3 4
3 2 5

输出样例 2:

Impossible

题意 : 问你最短工期是多少,那么想想这个最短工期要怎么得到,这些全部的任务组成一个项目,那么就是需要我们去完成全部的任务,并且需要只有当前任务完成后才能去开始下一个任务。
那么我们就可以把完成任务所需最多的时间当成最短的时间即可。
代码示例 :
#define ll long long
const int maxn = 1e6+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f; int n, m;
struct node
{
int to, cost;
node(int _to=0, int _cost=0):to(_to), cost(_cost){}
};
vector<node>ve[105];
int deg[105], ans[105];
queue<int>que;
int cnt = 0, an = 0; void fun(){
while(!que.empty()){
int v = que.front();
que.pop();
cnt++; for(int i = 0; i < ve[v].size(); i++){
int to = ve[v][i].to;
int cost = ve[v][i].cost;
deg[to]--;
if (!deg[to]) que.push(to);
ans[to] = max(ans[to], ans[v]+cost);
an = max(an, ans[to]);
}
}
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int x, y, c; cin >> n >> m;
for(int i = 1; i <= m; i++){
scanf("%d%d%d", &x, &y, &c);
ve[x].push_back(node(y, c));
deg[y]++;
}
for(int i = 0; i < n; i++) {
if (!deg[i]) que.push(i);
}
fun();
if (cnt != n) {
printf("Impossible\n");
}
else printf("%d\n", an);
return 0;
}

PTA - 拓扑排序的更多相关文章

  1. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  2. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  3. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  4. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  5. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  6. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  7. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  8. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

  9. *HDU1285 拓扑排序

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. springboot整合mybatis完整示例, mapper注解方式和xml配置文件方式实现(我们要优雅地编程)

    一.注解方式 pom <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId& ...

  2. Python--day60--jinjia2模块

  3. 解决input number类型上下滚动 禁用滚轮事件

    1.去掉input在type="number"时的上下箭头 <style> input::-webkit-outer-spin-button,input::-webki ...

  4. eclipse本地启动tomcat报错集锦

    1.eclipse本地添加tomcat服务器    打开Eclipse,单击“window”菜单,选择下方的“Preferences”: 找到Server下方的Runtime Environment, ...

  5. java GUI(图形用户界面)

    GUI Graphical User Interface(图形用户接口). 用图形的方式,来显示计算机操作的界面,这样更方便更直观. CLI Command line User Interface ( ...

  6. 12627 - Erratic Expansion——[递归]

    Piotr found a magical box in heaven. Its magic power is that if you place any red balloon inside it ...

  7. P1048 数组中的逆序对

    题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 输入格式 第一行包含一个整数 \(n\) ,表示数组中的元素个数 ...

  8. 关于electron中入口文件main.js一些重要参数(持续更新maybe)

    const {app, BrowserWindow} = require('electron') const path = require('path') let mainWindow functio ...

  9. 判断移动端还是PC端

    window.onload=function(){ var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad = sUserAge ...

  10. from __future__ import print_function的使用

    1.作用:把下一个新版本的特性导入到当前版本,就可以在当前版本中测试一些新版本的语法特性,例如在python2的环境下加入这一句可以测试python3的输出语法 2.使用方式:置于程序的第一行 3.示 ...