7-12 How Long Does It Take
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的更多相关文章
- python 各模块
01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- 在mybatis中写sql语句的一些体会
本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...
- AndroidStudio — Error:Failed to resolve: junit:junit:4.12错误解决
原博客:http://blog.csdn.net/u013443865/article/details/50243193 最近使用AndroidStudio出现以下问题: 解决:打开app下的buil ...
- 读过MBA的CEO更自私?《哈佛商业评论》2016年第12期。4星
老牌管理杂志.每期都值得精度.本期我还是给4星. 以下是本书中的一些内容的摘抄: 1:他们发现在Airbnb上,如果客人姓名听起来像黑人,那么比名字像白人的客人的接受率会低16%.#45 2:对立组织 ...
- 12个小技巧,让你高效使用Eclipse
集成开发环境(IDE)让应用开发更加容易.它们强调语法,让你知道是否你存在编译错误,在众多的其他事情中允许你单步调试代码.像所有的IDE一 样,Eclipse也有快捷键和小工具,这些会让您感觉轻松许多 ...
- 第12章 Linux系统管理
1. 进程管理 1.1 进程查看 (1)进程简介 进程是正在执行的一个程序或命令(如ls命令也是一个进程),每个进程都是一个运行的实体,都有自己的地址空间,并占用一定的系统资源. (2)进程管理的作用 ...
- Jexus Web Server 完全傻瓜化图文配置教程(基于Ubuntu 12.04.3 64位)[内含Hyper-v 2012虚拟机镜像下载地址]
1. 前言 近日有感许多新朋友想尝试使用Jexus,不过绝大多数都困惑徘徊在Linux如何安装啊,如何编译Mono啊,如何配置Jexus啊...等等基础问题,于是昨日向宇内流云兄提议,不如搞几个配置好 ...
- CSharpGL(12)用T4模板生成CSSL及其renderer代码
CSharpGL(12)用T4模板生成CSSL及其renderer代码 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含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.报错this.getUserInfo(this.setData) is not a function;at pages/index/index onShow function;at api req ...
- JavaScript 练习题
练习题 1. 使用for循环输出1到50的值,要求每次循环只能输出一个值,每输出十个换一行. 2 日历生成器: 要求 用户输入,这个月有多少天,本月1号是星期几,自动生成日历 3. 表格生成器 4. ...
- Android.HowToDesignPluginArchitectureInAndroidApp
There is a tools called "dx", this tool can transfer Java Binary Code into Android Dalvik ...
- svn 回滚文件修改
取消对代码的修改分为两种情况: 第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn revert [ ...
- 生活类App原型制作分享-AnyList
AnyList是一款可以帮你创建购物清单,并且帮助你整理食谱的生活工具App,前面引导页采用图片+文字的方式,介绍App的用法,登录注册采用选项卡切换的方式,减少了页面切换的繁琐操作,在Mockplu ...
- 轻博客类Web原型制作分享——Tumblr
Tumblr(汤博乐)成立于2007年,是目前全球最大的轻博客网站,也是轻博客网站的始祖. Tumblr是一种介于传统博客和微博之间的全新媒体形态,既注重表达,又注重社交,而且注重个性化设置,成为当前 ...
- sql server profiler 工具使用 sql 语句性能分析
https://www.cnblogs.com/knowledgesea/p/3683505.html
- Nodejs+Mongo+WebAPI
Nodejs+Mongo+WebAPI集成 1.[ 目录]: |- models/bear.js |- node_modules/ |- express |- mongoose |- body-par ...
- CRC标准以及简记式
一.CRC标准 下表中列出了一些见于标准的CRC资料: 名称 生成多项式 简记式* 应用举例 CRC-4 x4+x+1 3 ITU G.704 CRC-8 x8+x5+x4+1 31 DS18B20 ...
- response输出随机图片、定时刷新网页
第一招:利用response向浏览器输出图片: //获取验证码 在<img />标签内的src属性设为请求路径/verifyCode?goodsId=xxx&token=xxxxx ...