SDUTOJ 2498 数据结构实验之图论十一:AOE网上的关键路径
题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2498.html
题目大意
分析
代码如下
#include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
#define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
#define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,0x3f,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T>
ostream &operator<<(ostream &out, vector<T> &v) {
Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
return out;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef vector< int > VI;
typedef vector< bool > VB;
typedef vector< char > VC;
typedef vector< double > VD;
typedef vector< string > VS;
typedef vector< LL > VL;
typedef vector< VI > VVI;
typedef vector< VB > VVB;
typedef vector< VS > VVS;
typedef vector< VL > VVL;
typedef vector< VVI > VVVI;
typedef vector< VVL > VVVL;
typedef pair< int, int > PII;
typedef pair< LL, LL > PLL;
typedef pair< int, string > PIS;
typedef pair< string, int > PSI;
typedef pair< string, string > PSS;
typedef pair< double, double > PDD;
typedef vector< PII > VPII;
typedef vector< PLL > VPLL;
typedef vector< VPII > VVPII;
typedef vector< VPLL > VVPLL;
typedef vector< VS > VVS;
typedef map< int, int > MII;
typedef unordered_map< int, int > uMII;
typedef map< LL, LL > MLL;
typedef map< string, int > MSI;
typedef map< int, string > MIS;
typedef set< int > SI;
typedef stack< int > SKI;
typedef queue< int > QI;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e4 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Edge{
int from, to, w;
int et, lt;
}; istream& operator>> (istream& in, Edge &x) {
in >> x.from >> x.to >> x.w;
return in;
} struct Vertex{
int in, out, value, nxt;
VI next, prev;
int et, lt; void clear() {
value = in = out = ;
nxt = inf;
next.clear();
prev.clear();
et = ;
lt = inf;
}
}; int N, M, S, T;
Vertex V[maxN];
vector< Edge > E;
VI topo, ans; void addEdge(Edge &x) {
V[x.from].next.PB(E.size());
V[x.to].prev.PB(E.size());
++V[x.to].in;
++V[x.from].out;
E.PB(x);
} void init() {
For(i, , N) V[i].clear();
E.clear();
topo.clear();
ans.clear();
} void TopoSort() {
SKI sk;
For(i, , N) if(!V[i].in) sk.push(i); while(!sk.empty()) {
int tmp = sk.top(); sk.pop(); topo.PB(tmp);
Rep(i, V[tmp].next.size()) {
Edge &e = E[V[tmp].next[i]];
if(!--V[e.to].in) sk.push(e.to);
}
}
} // 在拓扑排序的基础上松弛
inline void relax() {
rFor(i, topo.size() - , ) {
Rep(j, V[topo[i]].prev.size()) {
Edge &e = E[V[topo[i]].prev[j]];
if(V[e.from].value < V[e.to].value + e.w || V[e.from].value == V[e.to].value + e.w && e.to < V[e.from].nxt) {
V[e.from].value = V[e.to].value + e.w;
V[e.from].nxt = e.to;
}
}
}
} int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
INIT();
while(cin >> N >> M) {
init();
For(i, , M) {
Edge t;
cin >> t;
addEdge(t);
} TopoSort();
S = topo.front();
T = topo.back(); relax(); int t = S;
while(t != inf) {
ans.PB(t);
t = V[t].nxt;
} cout << V[S].value << endl;
Rep(i, ans.size() - ) cout << ans[i] << " " << ans[i + ] << endl;
}
return ;
}
SDUTOJ 2498 数据结构实验之图论十一:AOE网上的关键路径的更多相关文章
- 数据结构实验之图论十一:AOE网上的关键路径【Bellman_Ford算法】
Problem Description 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图. AOE(Activity On Edge)网:顾名思义,用边 ...
- SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列
数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...
- SDUT OJ 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之图论六:村村通公路(最小生成树)
数据结构实验之图论六:村村通公路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...
- SDUT OJ 数据结构实验之图论四:迷宫探索
数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- 数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )
数据结构实验之图论七:驴友计划 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT 3363 数据结构实验之图论七:驴友计划
数据结构实验之图论七:驴友计划 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 做为一个资深 ...
- SDUT 3364 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 在哥尼斯堡的 ...
随机推荐
- PHP面试 PHP基础知识 十一(开发环境及相关配置)
开发环境及相关配置 版本控制软件 集中式和分布式 集中式:集中式版本控制工具,版本库集中存放在中央服务器,团队成员里的每个人工作时从中央服务器下载代码,个人修改后再提交到中央服务器 分布式:分布式版本 ...
- 基于turtle库的七段数码管绘制
·文章结构 >样例及概览 >函数框架分析 >功能发展·样例及概览 七段数码管,是信号灯.电子表等很多设备的显示形式.而利用python的turtle库,我们也可以模拟着写出一个动态生 ...
- camunda授权的一些简单操作
/** * 授权操作 */public class ZccAuthorizationService { AuthorizationService authorizationService; @Befo ...
- 比较全面的CSS hack方式一览
转载请注明来自CSDN freshlover的博客专栏<史上最全CSS Hack方式一览> 做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某 ...
- tushare下载安装教程与版本更新步骤
使用前提 安装Python 安装pandas:pip install pandas 安装lxml:pip install lxml 下载安装 方式1:pip install tushare,如果安装网 ...
- 42-python基础-python3-字符串-原始字符串
原始字符串 可以在字符串开始的引号之前加上 r,使它成为原始字符串. “原始字符串”完全忽略所有的转义字符,打印出字符串中所有的倒斜杠. 如果输入的字符串包含许多倒斜杠,比如正则表达式字符串,那么原始 ...
- selinux与kernel 0day
selinux与kernel 0day kernel NULL pointer的利用需要把shellcode映射到内存0处, 大家在测试exp的时候,总能发现一个规律, 开着selinux就能溢出成功 ...
- springboot Service层单元测试
两个实现类实现同一个Service接口 public interface CustomUrlService { List<ShopMetrics> getShopMetrics(); } ...
- linux执行wget url时提示“无法建立 SSL 连接”
linux执行wget url时提示“无法建立 SSL 连接” 原因: wget在使用HTTPS协议时,默认会去验证网站的证书,而这个证书验证经常会失败 解决方案: 1.加上参数“--no-check ...
- 【转载】网易极客战记官方攻略-地牢-严酷考验 B
关卡连接:https://codecombat.163.com/play/level/the-gauntlet-b 免费试玩:https://codecombat.163.com/play 使用你的所 ...