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. rsync同步工具的配置与使用

    一.什么是rsync?rsync是一款开源的,快速的,多功能的,可实现全量及增量的本地或远程数据同步备份的优秀工具. rsync官网 http://rsync.samba.org/ 二.rsync的工 ...

  2. php ActiveMQ的安装与使用

    一.ActiveMQ是什么?ActiveMQ是Apache出品,最流行的,能力强劲的开源消息总线.支持多种语言客户端(Java,C,C++,C#,Python,Ruby,Perl,PHP), 支持多种 ...

  3. Hibernate: save, persist, update, merge, saveOrUpdate[z]

    [z]https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate 1. Introduction In this ...

  4. [z]规则引擎

    https://www.ibm.com/developerworks/cn/java/j-drools/ 使用声明性编程方法编写程序的业务逻辑 使用规则引擎可以通过降低实现复杂业务逻辑的组件的复杂性, ...

  5. 转 移动端-webkit-user-select:none导致input/textarea输入框无法输入

    移动端webview中写页面的时候发现个别Android机型会导致input.textareat输入框无法输入(键盘可以弹起,不是webView.requestFocus(View.FOCUS_DOW ...

  6. andorid 计算器

    avtivity_main.xml <?xml version="1.0" encoding="utf-8"?> <GridLayout xm ...

  7. String 练习

    package com.hanqi; import java.util.Random; public class Text { public static void main(String[] arg ...

  8. MySQL优化(一) 优化关键技术

    MySql的优化是一个综合性的技术,主要包括有: (1)表的设计合理化(符合 3NF 三范式) (2)添加适当的索引(Index):索引分类:普通索引.主键索引.唯一索引.全文索引(文本).空间索引. ...

  9. HDOJ4261 Estimation

    一道需要用堆初始化的\(DP\) 原题链接 显然对于每一个部分,当\(b[i]\)为\(a\)对于部分的中位数时,差错最小.设\(S(x,y)\)表示\(x\sim y\)这一部分的差错. \(DP\ ...

  10. 10分钟搭建 App 主流框架

    搭建主流框架界面 0.达成效果 我们玩iPhone应用的时候,有没发现大部分的应用都是上图差不多的结构,下面的TabBar控制器可以切换子控制器,上面又有Navigation导航条 我们本文主要是搭建 ...