MOOC PTA 08-图8 How Long Does It Take
2015-10-31 23:03
435 查看
http://pta.patest.cn/pta/test/18/exam/4/question/631
构建图的邻接矩阵,寻找入度为0的顶点,将其压入队列,出队列时对其相连接的顶点入度减1,更新每个顶点的最大时间。
刚开始提交 3和5 测试点过不去 是因为我以为输出最后一个顶点就是工程的最后一个顶点
构建图的邻接矩阵,寻找入度为0的顶点,将其压入队列,出队列时对其相连接的顶点入度减1,更新每个顶点的最大时间。
刚开始提交 3和5 测试点过不去 是因为我以为输出最后一个顶点就是工程的最后一个顶点
#include<vector> #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cctype> #include<cmath> #include<cstdlib> #include<queue> using namespace std; #define MAXnum 100 #define INF 100001 #define max(a,b) a>b?a:b int Nv;//顶点个数 int Ne;//边的个数 int G[MAXnum][MAXnum];//图的邻接矩阵 int indegree[MAXnum];//入度计算 int time1[MAXnum];//时间计算 int countv; void BuildGraph() { int i,j,v1,v2,w; scanf("%d",&Nv); for(i=0; i<Nv; i++) for(j=0; j<Nv; j++) G[i][j]=-1; for( i=0; i<Nv; i++) { indegree[i]=0; time1[i]=0; } scanf("%d",&Ne); for(i=0; i<Ne; i++) { scanf("%d%d%d",&v1,&v2,&w); G[v1][v2]=w; indegree[v2]++; time1[v2]=max(w,time1[v2]); } } void topsort() { queue<int> q; int v; int i; countv=0; for(i=0; i<Nv; i++) if(!indegree[i]) q.push(i); while(!q.empty()) { v=q.front(); countv++; q.pop(); for( i=0; i<Nv; i++) if(G[v][i]!=-1&&indegree[i]>0) { if(--indegree[i]==0) q.push(i); time1[i]=max(G[v][i]+time1[v],time1[i]); } } if(countv<Nv) printf("Impossible\n"); else { int max1=-1; for(i=0; i<Nv; i++) if(max1<time1[i]) max1=time1[i]; printf("%d\n",max1); } } int main() { BuildGraph(); topsort(); return 0; }
相关文章推荐
- tyvj1079数字三角形3
- 一组数据中只有一个数字出现一次,其他数成对出现,找出这个数
- Memcached入门(一)
- cvStereoRectify 报错
- NSFileManager文件操作
- Servlet 生命周期、工作原理
- URL、URI和URN三者之间的区别
- 使用 openssl 命令行构建 CA \b及证书(一)
- tyvj1044数字三角形
- LightOJ - 1060 nth Permutation(计数)
- 英语学习重要性
- wget 转
- java 发展简史
- hdu 4856 tunnel(bfs+状态压缩dp)
- CodeForces 589F Gourmet and Banquet(二分 + 最大流)
- LightOJ - 1110 An Easy LCS(LCS)
- Game(bc模拟题)
- codevs2597团伙
- Oracle实战——简介、安装与配置
- linux多线程编程--信号量和条件变量 唤醒丢失事件