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是这 ...
随机推荐
- block代码块介绍
关于block的简单介绍 什么是block? Block是C语言的一个语法特性,同时也是C语言的运行时特性,它很像C中的函数指针,因为你可以像使用函数指针一样的去使用block对象:它也很像C++中的 ...
- ios中的界面跳转方式
ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...
- java中调用js脚本
JDK1.6加入了对Script(JSR223)的支持.这是一个脚本框架,提供了让脚本语言来访问Java内部的方法.你可以在运行的时候找到脚本引擎,然后调用这个引擎去执行脚本.这个脚本API允许你为脚 ...
- Python之路【第十三篇】:jQuery -暂无内容-待更新
Python之路[第十三篇]:jQuery -暂无内容-待更新
- VS2015 Cordova Ionic移动开发(三)
一.基础设置 1.修改App名称和程序起始页 打开config.xml配置文件显示如下,在[通用]选项卡中,将显示名称和起始页,修改为自己想要的名称即可. 如需直接在xml文件中修改:右击config ...
- xceed wpf datagrid
<!--*********************************************************************************** Extended ...
- oracle还原数据库及遇到的问题
1. 第一:用安装数据库时的管理员用户登录:创建一个新的用户,如: //创建用户123密码456 create user 123 identified by 456;第二:授权,赋予dba的权限 gr ...
- Oracle主键自动生成_表and存储过程
-- Create table create table T_EB_SYS_DN_SEQUENCE_CONFIG ( sequence_id VARCHAR2(36) default sys_guid ...
- angular.js 数字
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
- js 模板引擎 - 超级强大
本来没想写这篇文章,但是网上误导大众的文章太多了,所以今天就抽出半小时时间谈一下我对前端模板引擎的感受吧. 前端模板引擎相信大家都再熟悉不过了,市面上非常多的号称最好.最快.最牛逼的,随便就能找到一大 ...