AOE网上的关键路径(最长路径 + 打印路径)
题目描述
一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图。
AOE(Activity On Edge)网:顾名思义,用边表示活动的网,当然它也是DAG。与AOV不同,活动都表示在了边上,如下图所示:


如上所示,共有11项活动(11条边),9个事件(9个顶点)。整个工程只有一个开始点和一个完成点。即只有一个入度为零的点(源点)和只有一个出度为零的点(汇点)。
关键路径:是从开始点到完成点的最长路径的长度。路径的长度是边上活动耗费的时间。如上图所示,1 到2 到 5到7到9是关键路径(关键路径不止一条,请输出字典序最小的),权值的和为18。
输入
输出
示例输入
9 11
1 2 6
1 3 4
1 4 5
2 5 1
3 5 1
4 6 2
5 7 9
5 8 7
6 8 4
8 9 4
7 9 2
示例输出
18
1 2
2 5
5 7
7 9 好久做过的题了,不过今天拿出来看看觉得这个题挺好,它是逆向建图和打印路径,求最长路径;
最长路径用SPFA求解,但注意初始化为最小。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<vector>
using namespace std; const int INF = 0x3f3f3f3f;
const int MAX = ; struct edge
{
int to,w;
}; struct node
{
int indegree;
int outdegree;
}V[MAX+]; int n,m;
int inque[MAX+],dis[MAX+];
int pre[MAX+];
vector<edge>map[MAX+]; void spfa(int s)
{
queue<int>que;
memset(inque,,sizeof(inque));
for(int i = ; i <= n; i++)
dis[i] = -INF;//注意初始化
dis[s] = ;
que.push(s);
inque[s] = ;
while(!que.empty())
{
int u = que.front();
que.pop();
inque[u] = ;
for(int i = ; i < map[u].size(); i++)
{
int to = map[u][i].to;
if(dis[u] > -INF && (dis[to] < map[u][i].w + dis[u] || (dis[to] == map[u][i].w + dis[u] && u < pre[to])))
{
dis[to] = map[u][i].w + dis[u];
pre[to] = u;
if(inque[to] == )
{
que.push(to);
inque[to] = ;
}
}
}
}
} int main()
{
int u,v,w,s,t;
while(~scanf("%d %d",&n,&m))
{
memset(pre, 0x3f, sizeof(pre));
for(int i = ; i <= n; i++)
map[i].clear();
for(int i = ; i <= n; i++)
{
V[i].indegree = ;
V[i].outdegree = ;
}
for(int i = ; i <= m; i++)
{
//逆向建图
scanf("%d %d %d",&u,&v,&w);
map[v].push_back((struct edge){u, w});
V[v].outdegree++;
V[u].indegree++;
}
for(int i = ; i <= n; i++)
{
if(V[i].indegree == )
s = i;
if(V[i].outdegree == )
t = i;
}
spfa(s);
printf("%d\n",dis[t]);
int x = t;
//打印路径
while(x != s)
{
printf("%d %d\n", x, pre[x]);
x = pre[x];
}
}
return ;
}
AOE网上的关键路径(最长路径 + 打印路径)的更多相关文章
- SDUT 2498 AOE网上的关键路径
AOE网上的关键路径 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 一个无环的有向图称为无 ...
- sdut AOE网上的关键路径(spfa+前向星)
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2498&cid=1304 题目描述 一个无环的有向图称为无环图(Directed Acyc ...
- 数据结构实验之图论十一:AOE网上的关键路径【Bellman_Ford算法】
Problem Description 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图. AOE(Activity On Edge)网:顾名思义,用边 ...
- SDUTOJ 2498 数据结构实验之图论十一:AOE网上的关键路径
题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2498.html 题目大意 略. 分析 ...
- sdut 2498【aoe 网上的关键路径】
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2498 代码超时怎么破: #include< ...
- SDUT 2498-AOE网上的关键路径(spfa+字典序路径)
AOE网上的关键路径 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 一个无环的有向图称为无环图(Directed Acycl ...
- SDUT-2498_AOE网上的关键路径
数据结构实验之图论十一:AOE网上的关键路径 Time Limit: 2000 ms Memory Limit: 65536 KiB Problem Description 一个无环的有向图称为无环图 ...
- zoj 3088 Easter Holidays(最长路+最短路+打印路径)
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...
- 最长公共子序列Lcs(打印路径)
给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是,abca也是,其中abca是这 ...
随机推荐
- windows下apache+php+mysql配置
Apache 2.4.10(文件:httpd-2.4.10-win64-VC11.zip) php 5.6.26 (文件:php-5.6.25-Win32-VC11-x64.zip) mysql 5. ...
- vs在winform中不给力哈-错误不提示
我的操作系统是windows Server 2008 x64,运行winform的时候,对Dictionary累加值.运行的时候,项目一闪而过,于是我在Project的Properties上选择运行的 ...
- [转]Web UI 设计命名规范
来源:http://blog.bingo929.com/web-ui-design-name-convention.html 一.网站设计及基本框架结构: 1. Container “conta ...
- 用PHP实现一个高效安全的ftp服务器(二)
接前文. 1.实现用户类CUser. 用户的存储采用文本形式,将用户数组进行json编码. 用户文件格式: * array( * 'user1' => array( * 'pass'=>' ...
- C#winform程序自定义鼠标样式
public void SetCursor(Bitmap cursor, Point hotPoint) { int hotX = hotPoint.X; int hotY = hotPoint.Y; ...
- Ubuntu 12.04下解决Rhythmbox Music Player乱码问题
1.打开终端输入如下信息: $ sudo gedit ~/.profile 2.在打开的文档末尾加上如下两句: export GST_ID3_TAG_ENCODING=GBK:UTF-8:GB1803 ...
- PHP算法 《图》 之 理论基础
转载自:http://www.cnblogs.com/skywang12345/p/3691463.html Ⅰ 图的基本概念 1. 图的定义 定义:图(graph)是由一些点(vertex)和这些点 ...
- npm不能安装任何包,报错:npm WARN onload-script failed to require onload script npm-autoinit/autoinit及解决方法
想要利用Hexo搭建一个博客,但是安装时npm一直报错,不仅仅是Hexo包,连别的其他包也不行,会提示下面的一堆错误 npm WARN onload-script failed to require ...
- Template 模式
Template 模式是很简单模式,但是也应用很广的模式.Template 是采用继承的方式实现算法的异构,其关键点就是将通用算法封装在抽象基类中,并将不同的算法细节放到子类中实现.Template ...
- location传值
location.href="url?p="+"value"; onclick="location.href='Card_query_where?qu ...