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. 【WPF】2、美化控件

    控件有默认样式,但是有时候默认样式并不够用,就需要美化. 1.常用的方法是美术出图,直接贴图进去,效果又好又简单(对程序来说). 用图片有三种方式:设置控件背景图片.设置控件内容为图片和直接使用图片做 ...

  2. 思维导图xmind的文档保存问题

    如果文件名相同,可能最新的文档覆盖以前的.当前活动文档只能有一个,如果有多个,保存后,其他活动文档也被更新了. 新建一个空白doc文档,仅仅是文件名,作为附件导入到xmind中,在xmind中保存后, ...

  3. Linux命令:scp

    1.find命令: scp [参数] [原路径] [目标路径] 2.用法: scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令.linux的sc ...

  4. Linux 部署 YUM 仓库

    本篇主要写了怎么搭建自定义的YUM源,在一个拥有大量本地网络的主机环境中,可以减少对外网的依赖. Server 安装 vsftp 包 [root@server ~]# yum install vsft ...

  5. mysql 触发器、流程控制、事务等

    视图 触发器 事务 存储过程 内置函数 流程控制 索引 视图 1.什么是视图 ​ 视图就是通过查询得到一张虚拟表,然后保存下来,下次直接使用即可 2.为什么要用视图 ​ 如果要频繁使用一张虚拟表,可以 ...

  6. 常用Windows命令、常用 Cmd命令(补充)

    常用的Windows 命令使用能够提升工作效率以及快捷处理事项. 下面为平时常用的Windows 命令/cmd 命令. 一.以下命令无需打开cmd 窗口即可操作(输入完毕 打个 回车,即可执行). 1 ...

  7. 【解决】挂载NFS服务时,不同共享客户端间的数据不同步

    问题现象 当您用台 ECS 挂载同一个 NFS 文件系统,在 ECS-A 上 append 写文件,在 ECS-B 用 tail -f 观察文件内容的变化.在 ECS-A 写完之后,在 ECS-B 看 ...

  8. docker中使用阿里云的apt源安装各种实用工具

    今天想在docker中安装vim工具,还有其他的软件等等,如果你直接执行apt-get install vim是没有用的,会显示: root@7d43d83fd3a8:/etc/nginx# apt- ...

  9. 卓越Code团队SCRUM呕心沥血实践总结

    卓越Code团队SCRUM呕心沥血实践总结 序言 所属课程 https://edu.cnblogs.com/campus/xnsy/2019autumnsystemanalysisanddesign ...

  10. 性能测试基础---联机负载&IP欺骗

    ·联机负载&IP欺骗 ·联机负载:又叫分布式负载,即通过多台负载机(压力机)运行脚本,向服务器发送请求,从而实现更多的负载压力. ·联机负载的具体操作: ·了解两个概念: ·控制机:所谓控制机 ...