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

输入格式:

首先第一行给出两个正整数:项目里程碑的数量 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;
}

最新文章

  1. CSS篇之DIV+CSS布局
  2. JAVA NIO系列(四) 选择器
  3. jquery的常用ajax操作
  4. 写了个Linux包过滤防火墙
  5. mysql分表与分区表
  6. #pragma once
  7. 安装aptana插件报Error opening the editor. java.lang.NullPointerException
  8. 如何写一个像btgoogle一样的12306泄露数据查询
  9. Android Wear 数据类型和接口的发送和同步数据概述
  10. Rabin-Karp字符串查找算法
  11. 自学Zabbix3.6.1-触发器triggers创建
  12. Oracle复制表结构及数据
  13. Mycat 分片规则详解--自然月分片
  14. VirtualBox下安装linux虚拟机
  15. Python 回溯算法
  16. .NET基础之构造函数
  17. python3 邮件发送
  18. 统计硬币 HDU - 2566 (三种解法:线性代数解法,背包解法,奇思妙想解法 &gt;_&lt; )
  19. MPI之聚合通信-Scatter,Gather,Allgather
  20. 浏览器 worker

热门文章

  1. 1625 - Color Length——[动态规划]
  2. Codeforces Round #564 (Div. 2) D. Nauuo and Circle(树形DP)
  3. P1093 铺地毯
  4. 2019-9-22-dotnet-core-导出-COM-组件
  5. H3C网络监测工具命令
  6. vue 实例未加载完成显示 花括号解决方案
  7. javascript拷贝
  8. 基于vs2015 SignalR开发的微信小程序使用websocket实现聊天功能
  9. Team Foundation Server 2015使用教程【5】:默认团队checkin权限修改
  10. 使用 HttpClient 进行表单提交时,遇到的问题