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 (≤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 分)的更多相关文章
- 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 ...
随机推荐
- linuxmint安装Tools找不到Tools的压缩包问题
安装Linuxmint之后按照惯例安装Tools,打开桌面上的Tools光盘之后找不到压缩包. PS:因为已经装好了,就不上图了,按照下面的步骤做就没有问题了. 1:找到vmware的安装目录下的li ...
- java List深拷贝示例
示例一 import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java ...
- Django-xadmin的使用介绍
Django-xadmin的介绍 Django是python的重量级web框架,写得少,做得多,非常适合后端开发,它很大的一个亮点是,自带后台管理模块,但它自带的后台管理有点丑,而Xadmin是基于b ...
- Django---Django的中间件
Django---Django的中间件 一丶中间件介绍 什么是中间件 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Dj ...
- java 之 集合概述
一.集合概述 不管是哪一种数据结构,其实本质上都是容器来着,就是用来装对象的.因此,我们就要搞清楚两点:(1)如何存储(2)存储特点 1.集合 集合是 Java 中提供的一种容器,可以用来存储多个数据 ...
- Linux系统MySQL的常用操作命令
安装好MySQL服务后添加环境变量: #vi /etc/profile export MYSQL_HOME=/usr/local/mysql export PATH=$PATH:$MYSQL_HOME ...
- 数据结构与算法16—平衡二叉(AVL)树
我们知道,对于一般的二叉搜索树(Binary Search Tree),其期望高度(即为一棵平衡树时)为log2n,其各操作的时间复杂度O(log2n)同时也由此而决定.但是,在某些极端的情况下(如在 ...
- k8s 初识pod (二)
kubernetes中调用pod到哪个节点上是无关紧要的,但由于实际情况,每台node的硬件环境不一致,所以某些情况要求将不同pod调到指定节点上运行.也可以通过label实现. kubectl la ...
- Httpd服务入门知识-Httpd服务安装
Httpd服务入门知识-Httpd服务安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Httpd概述 1>.Httpd介绍 20世纪90年代初,国家超级计算机应用中心 ...
- Centos7永久修改IP地址(NAT模式)
永久修改IP地址,即为设置静态的IP地址. 一.修改IP地址前需要准备的工作 1.虚拟机需要使用NAT的网络模式 虚拟机关机状态下,点击"编辑虚拟机设置",点击"网络适配 ...