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".

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
#include<stdio.h>
#include<queue>
using namespace std;
const int maxn = ; int map[maxn][maxn],d[maxn];
int inDegree[maxn]; void init(int n); int main()
{
int n,m;
scanf("%d%d",&n,&m); init(n); int u,v,w;
for (int i = ; i < m; i++)
{
scanf("%d%d%d",&u,&v,&w);
map[u][v] = w;
inDegree[v]++;
} queue<int> q; for (int i = ; i < n; i++)
{
if (!inDegree[i])
{
q.push(i);
d[i] = ;
}
} while (!q.empty())
{
int cur = q.front();
q.pop(); for (int i = ; i < n; i++)
{
if (map[cur][i] != -)
{
inDegree[i]--;
if (d[i] < d[cur] + map[cur][i])
{
d[i] = d[cur] + map[cur][i];
}
if (!inDegree[i])
{
q.push(i);
}
}
}
} int maxCost = -;
bool flag = true;
for (int i = ; i < n; i++)
{
if (inDegree[i])
{
flag = false;
break;
}
if (d[i] > maxCost)
{
maxCost = d[i];
}
} if (flag)
{
printf("%d",maxCost);
}
else
{
printf("Impossible");
} return ;
} void init(int n)
{
for (int i = ; i < n; i++)
{
d[i] = -;
inDegree[i] = ;
for (int j = ; j < n; j++)
{
map[i][j] = map[j][i] = -;
}
}
}
 

08-图8 How Long Does It Take (25 分)的更多相关文章

  1. PAT A1142 Maximal Clique (25 分)——图

    A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...

  2. 7-8 哈利·波特的考试(25 分)(图的最短路径Floyd算法)

    7-8 哈利·波特的考试(25 分) 哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变 ...

  3. PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  4. PAT A1134 Vertex Cover (25 分)——图遍历

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...

  5. PAT A1021 Deepest Root (25 分)——图的BFS,DFS

    A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on th ...

  6. PAT A1013 Battle Over Cities (25 分)——图遍历,联通块个数

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  7. L2-023 图着色问题 (25 分)vector

    图着色问题是一个著名的NP完全问题.给定无向图,,问可否用K种颜色为V中的每一个顶点分配一种颜色,使得不会有两个相邻顶点具有同一种颜色? 但本题并不是要你解决这个着色问题,而是对给定的一种颜色分配,请 ...

  8. PAT A1122 Hamiltonian Cycle (25 分)——图遍历

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  9. PAT A1150 Travelling Salesman Problem (25 分)——图的遍历

    The "travelling salesman problem" asks the following question: "Given a list of citie ...

  10. 1013 Battle Over Cities (25 分)(图的遍历or并查集)

    这题用并查集或者dfs都可以做 dfs #include<bits/stdc++.h> using namespace std; ; bool mp[N][N]; int n,m,k; b ...

随机推荐

  1. Kafka Streams的Data Types and Serialization

    Avro <repositories> <repository> <id>confluent</id> <url>http://packag ...

  2. asp获取access数据库中的一条随机记录

    针对“用一条SQL得到数据库中的随机记录集”问题在网上已经有很多答案了: SQL Server 2000: SELECT TOP n * FROM tanblename ORDER BY NEWID( ...

  3. 我的第一个netcore2.2 api项目搭建(一)

    早早就想入门netcore,一直没下定决心,这次正好碰上项目服务变更,便想着入坑试试,边学边用. 目标: 一.api使用core版的SqlSugar,集成orm,实现快速开发 二.api使用Swagg ...

  4. Django(二)模板

    一.模板概念 1.Django通过模板动态生成html 2.模板的加载位置 模板一般建立在templates文件夹中,全局路径的设置在settings.py中 ​ DIRS:决定了整个项目的模板路径的 ...

  5. Linux环境:VMware下windows虚拟机与linux主机进行文件共享的方法

    操作主要分两大步骤: 一.是对主机进行配置: 二.是在虚拟机上直接连接共享目录. 一.主机配置 1.打开VMware虚拟机,双击需要进行文件共享的虚拟机.如下图,双击CentOS 64位(以linux ...

  6. 【转】Webpack 快速上手(下)

    由于文章篇幅较长,为了更好的阅读体验,本文分为上.中.下三篇: 上篇介绍了什么是 webpack,为什么需要 webpack,webpack 的文件输入和输出 中篇介绍了 webpack 在输入和输出 ...

  7. 24、vuex刷新页面数据丢失解决办法

    刷新页面时候将state数据保存到localStorage里面: export default { name: 'App', created () { //在页面加载时读取localStorage里的 ...

  8. 解决selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in P

    转载 解决selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be i ...

  9. python基础-os模块

    os 模块 功能:与操作系统交互的模块 使用方式:import os 常用的几种功能 os.path.dirname(文件名) 用于获取当前文件的所在目录 import os # 获取当前文件的所在目 ...

  10. Python之路(第四十三篇)线程的生命周期、全局解释器锁

    一.线程的生命周期(新建.就绪.运行.阻塞和死亡) 当线程被创建并启动以后,它既不是一启动就进入执行状态的,也不是一直处于执行状态的,在线程的生命周期中,它要经过新建(new).就绪(Ready).运 ...