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 N (≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N−1), and M, the number of activities. Then M 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".

知识点:拓扑排序

思路:

建一个图‘pre’,记录每一个节点的前节点;建一个图‘Graph’,记录每一个节点的后节点

建一个队列,每次操作后,入度(indegree)为0的节点入队

  • 注意多个起点和终点的问题:
    • 完成后扫描最大的cost输出
  • 有回路问题
    • 遍历的节点数<总节点数,即有回路
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int maxn = ; int n,m;
struct edge{
int v;
int time;
};
vector<edge> pre[maxn];
vector<int> Graph[maxn];
int cost[maxn];
int indegree[maxn]; void check(int start,int end){
int cnt=;
fill(cost,cost+maxn,);
queue<int> q;
for(int i=;i<n;i++){ // indegree==0 的入队
if(indegree[i]==){
q.push(i);
}
}
int tmp;
while(q.size()){ // 按照拓扑序搜索
tmp = q.front();
cnt++;
q.pop();
cost[tmp] = ;
for(int i=;i<pre[tmp].size();i++){
if((pre[tmp][i].time+cost[pre[tmp][i].v]) > cost[tmp]){
cost[tmp] = (pre[tmp][i].time+cost[pre[tmp][i].v]);
}
}
for(int i=;i<Graph[tmp].size();i++){
indegree[Graph[tmp][i]]--;
if(indegree[Graph[tmp][i]]==){
q.push(Graph[tmp][i]);
}
}
}
int endtime = ;
if(cnt!=n) printf("Impossible\n");
else{
for(int i=;i<n;i++){
if(cost[i]>endtime){
endtime=cost[i];
}
}
printf("%d\n",endtime);
}
} int main(int argc, char *argv[]) {
scanf("%d %d",&n,&m);
int a,b,t;
struct edge newedge;
int flag[maxn];
fill(flag,flag+maxn,);
for(int i=;i<m;i++){
scanf("%d %d %d",&a,&b,&newedge.time);
indegree[b]++;
newedge.v = a;
flag[a] = ;
pre[b].push_back(newedge);
Graph[a].push_back(b);
}
int start,end;
for(int i=;i<n;i++){
if(flag[i]==){
end = i;
}
if(pre[i].size()==){
start = i;
}
}
//printf("%d %d",start,end);
check(start,end);
}

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

7-12 How Long Does It Take的更多相关文章

  1. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  2. Python Standard Library

    Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...

  3. 在mybatis中写sql语句的一些体会

    本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...

  4. AndroidStudio — Error:Failed to resolve: junit:junit:4.12错误解决

    原博客:http://blog.csdn.net/u013443865/article/details/50243193 最近使用AndroidStudio出现以下问题: 解决:打开app下的buil ...

  5. 读过MBA的CEO更自私?《哈佛商业评论》2016年第12期。4星

    老牌管理杂志.每期都值得精度.本期我还是给4星. 以下是本书中的一些内容的摘抄: 1:他们发现在Airbnb上,如果客人姓名听起来像黑人,那么比名字像白人的客人的接受率会低16%.#45 2:对立组织 ...

  6. 12个小技巧,让你高效使用Eclipse

    集成开发环境(IDE)让应用开发更加容易.它们强调语法,让你知道是否你存在编译错误,在众多的其他事情中允许你单步调试代码.像所有的IDE一 样,Eclipse也有快捷键和小工具,这些会让您感觉轻松许多 ...

  7. 第12章 Linux系统管理

    1. 进程管理 1.1 进程查看 (1)进程简介 进程是正在执行的一个程序或命令(如ls命令也是一个进程),每个进程都是一个运行的实体,都有自己的地址空间,并占用一定的系统资源. (2)进程管理的作用 ...

  8. Jexus Web Server 完全傻瓜化图文配置教程(基于Ubuntu 12.04.3 64位)[内含Hyper-v 2012虚拟机镜像下载地址]

    1. 前言 近日有感许多新朋友想尝试使用Jexus,不过绝大多数都困惑徘徊在Linux如何安装啊,如何编译Mono啊,如何配置Jexus啊...等等基础问题,于是昨日向宇内流云兄提议,不如搞几个配置好 ...

  9. CSharpGL(12)用T4模板生成CSSL及其renderer代码

    CSharpGL(12)用T4模板生成CSSL及其renderer代码 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立 ...

  10. ABP(现代ASP.NET样板开发框架)系列之12、ABP领域层——工作单元(Unit Of work)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之12.ABP领域层——工作单元(Unit Of work) ABP是“ASP.NET Boilerplate Pr ...

随机推荐

  1. PowerDesigner生成数据字典

    1.首先说明我使用的环境 2.打开PDM模型,右键-->NEW-->REPORT 3.数据字典编辑器(一些通用模型吧,自己可以再DIY) 4.选择Table-->List of Ta ...

  2. StackExchange.Redis使用以及封装

    来源:http://www.cnblogs.com/qtqq/p/5951201.html Redis安装:http://www.runoob.com/redis/redis-install.html ...

  3. 自定义 Mysql 类 与 自定义 异常类

    import MySQLdb class MyExcept(Exception): ''' 常见做法定义异常基类,然后在派生不同类型的异常 ''' def __init__(self, *args): ...

  4. Linux驱动之按键驱动编写(查询方式)

    在Linux驱动之LED驱动编写已经详细介绍了一个驱动的编写过程,接着来写一个按键驱动程序,主要是在file_operations结构中添加了一个read函数.还是分以下几步说明 1.查看原理图,确定 ...

  5. 团队作业之Rookie also want to fly

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 在时代的浪潮下,单人编程,结对编程已经无法满 ...

  6. Eclipse中的maven项目搭建

    一.eclipse中的maven设置 1.打开“首选项”----> "maven"---->"Installations".用来查看maven的使用 ...

  7. Mac下配置域名和网站测试环境

    一.在 /etc/hosts   下配置相关域名 1, control+space  打开spotlight, 搜索“terminal” 2, 打开Terminal 3, 在terminal界面中输入 ...

  8. Luogu 4234 最小差值生成树 - LCT 维护链信息

    Solution 将边从小到大排序, 添新边$(u, v)$时 若$u,v$不连通则直接添, 若连通则 把链上最小的边去掉 再添边. 若已经加入了 $N - 1$条边则更新答案. Code #incl ...

  9. 网页启用Gzip压缩 提高浏览速度

    启用Gzip压缩的好处 它的好处显而易见,提高网页浏览速度,无论是之前说的精简代码.压缩图片都不如启用Gzip来的实在.下图为启用Gzip后的效果. Gzip压缩效率非常高,通常可以达到70%的压缩率 ...

  10. NC 日志文件注册

    在实际开发中,例如接口向外系统发送数据,这些数据前台看不到,一般都是记录日志,然后在后台日志文件中查看.但是,用系统原本日志文件来看,有时会记录一些别的模块日志信息.所以,我们可以注册个自己的模块日志 ...