08-图8 How Long Does It Take (25 分
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 (≤), 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<cstdio>
#include<queue>
using namespace std;
const int maxn = ;
int map[maxn][maxn],d[maxn],indegree[maxn];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i = ; i < n; i++){
d[i] = -;
indegree[i] = ;
for(int j = ; j < n; j++){
map[i][j] = map[j][i] = -;
}
}
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] = ;
}
}
int cur;
while(!q.empty()){
cur = q.front();
q.pop();
for(int i = ; i < n; i++){
if(map[cur][i] != -){
indegree[i]--;
if(d[cur] + map[cur][i] > d[i]){
d[i] = d[cur] + map[cur][i];
}
if(!indegree[i]){
q.push(i);
}
}
}
}
int maxCost = -;
int i;
for(i = ; i < n; i++){
if(indegree[i]) break;
if(d[i] > maxCost) maxCost = d[i];
}
if(i == n) printf("%d",maxCost);
else printf("Impossible");
return ;
}
08-图8 How Long Does It Take (25 分的更多相关文章
- PAT A1142 Maximal Clique (25 分)——图
A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...
- 7-8 哈利·波特的考试(25 分)(图的最短路径Floyd算法)
7-8 哈利·波特的考试(25 分) 哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- L2-023 图着色问题 (25 分)vector
图着色问题是一个著名的NP完全问题.给定无向图,,问可否用K种颜色为V中的每一个顶点分配一种颜色,使得不会有两个相邻顶点具有同一种颜色? 但本题并不是要你解决这个着色问题,而是对给定的一种颜色分配,请 ...
- PAT A1122 Hamiltonian Cycle (25 分)——图遍历
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- 1013 Battle Over Cities (25 分)(图的遍历or并查集)
这题用并查集或者dfs都可以做 dfs #include<bits/stdc++.h> using namespace std; ; bool mp[N][N]; int n,m,k; b ...
随机推荐
- 洛谷 P1767 家族_NOI导刊2010普及(10)
题目描述 在一个与世隔绝的岛屿上,有一个有趣的现象:同一个家族的人家总是相邻的(这里的相邻是指东南西北四个方向),不同的家族之间总会有河流或是山丘隔绝,但同一个家族的人不一定有相同姓氏.现在给你岛上的 ...
- GCC生成动态库
main.c #include <stdio.h> void hello(void); int main(int argc, char ** argv) { printf("Th ...
- 【转】 Pro Android学习笔记(三八):Fragment(3):基础小例子-续
目录(?)[-] Step 2实现Fragment指定调用类TitleFragment onInflate和onAttach onCreate和onCreateView onActivityCreat ...
- 能否自己也写一个类叫做java.lang.String?
这次的随笔很逗吧~没错,我们的确也可以自己在创建一个包java.lang,然后在 相应的包下面创建一个对应的类String,但是在每次jre运行的时候,我们都回去加载原来默认的java.lang.St ...
- Frequent Pattern 挖掘之二(FP Growth算法)
Frequent Pattern 挖掘之二(FP Growth算法) FP树构造 FP Growth算法利用了巧妙的数据结构,大大降低了Aproir挖掘算法的代价,他不需要不断得生成候选项目队列和不断 ...
- 使用MySQL客户端登录Ensemble数据库查询相关信息
Ensemble公共MySQL数据库 对于大量数据和更详细的分析,Ensemble的MySQL服务器ensembldb.ensembl.org,useastdb.ensembl.org或asiadb. ...
- linux 环境变量恢复默认值
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin 在linux命令下如何访问一个ur ...
- position:fixed;如何居中
div{ position:fixed; margin:auto; left:; right:; top:; bottom:; width:100px; height:100px; } 如果只需要左右 ...
- 7.31实习培训日志-docker sql
docker mysql docker问题 今天问了老师有关docker的问题,entrypoint和cmd的区别,两者同时存在cmd的执行结果会被当做参数传入entrypoint. dockerfi ...
- 【转】Subversion快速入门教程-动画演示
如何快速建立Subversion服务器,并且在项目中使用起来,这是大家最关心的问题,与CVS相比,Subversion有更多的选择,也更加的容易,几个命令就可以建立一套服务器环境,可以使用起来,这里配 ...