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 ...
随机推荐
- golang 创建一个简单的连接池,减少频繁的创建与关闭
一.连接池的描述图片如下: 二.连接池代码如下: package main; import ( "time" "sync" "errors" ...
- IDEA使用过程中常见小问题
在磁盘随便一个位置,创建一个文件夹,可以通过IDEA open 打开它 然后在将一些代码模块,放入这个文件夹,IDEA刷新,能够自动显示出来 然后执行下面的文档操作 1.IDEA通过一个项目文件导 ...
- Vue filter-v-for 使用
var app5 = new Vue({ el: '#app5', data: { shoppingList: [ "Milk", "Donuts", &quo ...
- c# txt内存映射技术总结
对于大文件操作,readline 的方式读取文档,那操作起来跟蜗牛爬一样的慢了, 于是使用内存映射技术, 参考微软的这个使用方法说明 https://msdn.microsoft.com/zh-cn/ ...
- Oracle VM VirtualBox如何设置网络地址转换NAT
使用VirtualBox 安装好服务器后,需要设置网络,如果有IP, 则可以直接连接物理网络了, 如果没有,则可以直接使用NAT网络.设置方便快速. 先将虚拟机中的网络设置为自动获取,然后点击Virt ...
- php mysql 丢失更新
php mysql 丢失更新问题,搜索整个互联网,很少有讲到,也许和php程序员出身一般都是非科班出身有关系吧. 另外php程序一般都是简单数据,很少有并发一致性问题,所以大家都没有谁专门提出这个问题 ...
- Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用
Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...
- python文件读取操作、序列化
1.对文件进行读写操作时,先建立文件句柄 f = open("test.txt","r",encoding="UTF-8") 其中,r为文件 ...
- 基于tomcat的solr环境搭建(Linux)
♥♥ solr是基于lucene的一个全文检索服务器,提供了一些类似webservice的API接口,用户可以通过http请求solr服务器,进行索引的建立和索引的搜索.索引建立的过程:用户提交的文 ...
- ListView 删除item删除不了的问题解决办法
下面的方法是删除不了item的: Integer pos = Integer.valueOf(msg.getBody().toString()); adapter.getList().remove(p ...