题目描述

一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图。 
    AOE(Activity On Edge)网:顾名思义,用边表示活动的网,当然它也是DAG。与AOV不同,活动都表示在了边上,如下图所示:
                                     
    如上所示,共有11项活动(11条边),9个事件(9个顶点)。整个工程只有一个开始点和一个完成点。即只有一个入度为零的点(源点)和只有一个出度为零的点(汇点)。
    关键路径:是从开始点到完成点的最长路径的长度。路径的长度是边上活动耗费的时间。如上图所示,1 到2 到 5到7到9是关键路径(关键路径不止一条,请输出字典序最小的),权值的和为18。

输入

    这里有多组数据,保证不超过10组,保证只有一个源点和汇点。输入一个顶点数n(2<=n<=10000),边数m(1<=m <=50000),接下来m行,输入起点sv,终点ev,权值w(1<=sv,ev<=n,sv != ev,1<=w <=20)。数据保证图连通。

输出

    关键路径的权值和,并且从源点输出关键路径上的路径(如果有多条,请输出字典序最小的)。

示例输入

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网上的关键路径(最长路径 + 打印路径)的更多相关文章

  1. SDUT 2498 AOE网上的关键路径

    AOE网上的关键路径 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 一个无环的有向图称为无 ...

  2. sdut AOE网上的关键路径(spfa+前向星)

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2498&cid=1304 题目描述 一个无环的有向图称为无环图(Directed Acyc ...

  3. 数据结构实验之图论十一:AOE网上的关键路径【Bellman_Ford算法】

    Problem Description 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图.     AOE(Activity On Edge)网:顾名思义,用边 ...

  4. SDUTOJ 2498 数据结构实验之图论十一:AOE网上的关键路径

    题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2498.html 题目大意 略. 分析 ...

  5. sdut 2498【aoe 网上的关键路径】

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2498 代码超时怎么破: #include< ...

  6. SDUT 2498-AOE网上的关键路径(spfa+字典序路径)

    AOE网上的关键路径 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 一个无环的有向图称为无环图(Directed Acycl ...

  7. SDUT-2498_AOE网上的关键路径

    数据结构实验之图论十一:AOE网上的关键路径 Time Limit: 2000 ms Memory Limit: 65536 KiB Problem Description 一个无环的有向图称为无环图 ...

  8. zoj 3088 Easter Holidays(最长路+最短路+打印路径)

    Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...

  9. 最长公共子序列Lcs(打印路径)

    给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的).   比如两个串为:   abcicba abdkscab   ab是两个串的子序列,abc也是,abca也是,其中abca是这 ...

随机推荐

  1. IOS--实现滤镜效果的四种方式

    IOS–实现滤镜效果 demo地址: https://github.com/AbeDay/ios–.git 使用CIFilter来完成IOS中滤镜效果 在IOS中可以使用系统自带的方法来达到路径效果: ...

  2. WWDC-UIKit 中协议与值类型编程实战

    本文为 WWDC 2016 Session 419 的部分内容笔记.强烈推荐观看. 设计师来需求了 在我们的 App 中,通常需要自定义一些视图.例如下图: 我们可能会在很多地方用到右边为内容,左边有 ...

  3. 设置ViewController 数据源无法改变view

    病情描述: viewController创建的时候勾选了xib,然后在显示的时候调用了如下语句: MTDetailDealViewController *detailController = [[MT ...

  4. Python之路【第五篇】:面向对象和相关

    Python之路[第五篇]:面向对象及相关   面向对象基础 基础内容介绍详见一下两篇博文: 面向对象初级篇 面向对象进阶篇 其他相关 一.isinstance(obj, cls) 检查是否obj是否 ...

  5. ASP.NET Web API 文件產生器 - 使用 Swagger

    转帖:http://kevintsengtw.blogspot.hk/2015/12/aspnet-web-api-swagger.html Swagger 是一套 API 互動文件產生器,使用 HT ...

  6. Orace数据库锁表的处理与总结<摘抄与总结一>

    TM锁(表级锁)类型共有5种,分别称为共享锁(S锁).排它锁(X锁).行级共享锁(RS锁).行级排它锁(RX锁).共享行级排它锁(SRX锁) 当Oracle执行DML语句时,系统自动在所要操作的表上申 ...

  7. spring jdbctemplate调用procedure(返回游标)

    package cn.com.git.htsc.uac.core.repository.report; import cn.com.git.htsc.uac.core.api.dto.report.R ...

  8. AppiumDriver 运行app启动基本参数

    记录一下 DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(Mobile ...

  9. C#DataTable操作

    ] 在DataSet中添加DataTable DataSet.Tables.Add(DataTable) 实例: DataSet ds=new DataSet(); DataTable table=n ...

  10. jQuery api 学习笔记(1)

      之前自己的jquery知识库一直停留在1.4的版本,而目前jquery的版本已经更新到了1.10.2了,前天看到1.10中css()竟然扩充了那么多用法,这2天就迫不及待的更新一下自己的jquer ...