描述
求出所给的AOE-网的关键路径。
输入
若干行整数,第一行有2个数,分别为顶点数v和弧数a,接下来有a行,每一行有3个数,分别是该条弧所关联的两个顶点编号和弧的权值
输出
若干个空格隔开的顶点构成的序列(用小写字母)
样例输入
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
7 9 2
8 9 3
样例输出
v1 v2 v5 v7 v9
代码区:
#include <bits/stdc++.h>
using namespace std;
struct Edge{
int from;
int to;
//int e;
//int l;
int money;
};
class Grap{
private:
int vertexNum,edgeNum;
//int ljb; //邻接矩阵
//int Star,End;
Edge edge[120]; //边集数组
int ve[120],vl[120],ee[120],el[120];
public:
Grap(int n,int e);
// int path();
void time_1();
void time_2();
void time_3();
void time_4();
void key();
};
Grap::Grap(int n,int e){
memset(ve,0,sizeof(ve));
memset(vl,0,sizeof(vl));
memset(ee,0,sizeof(ee));
memset(el,0,sizeof(el));
vertexNum = n;
edgeNum = e;
for(int i=1; i<=e; ++i)
cin>>edge[i].from>>edge[i].to>>edge[i].money;
time_1();
time_2();
time_3();
time_4();
key();
}
void Grap::time_1(){
for(int i=1; i<=edgeNum; ++i){
int a = edge[i].from;
int b = edge[i].to;
int c = ve[a] + edge[i].money;
if(c>ve[b])
ve[b] = c;
}
/*
for(int i=1; i<=vertexNum; ++i){
cout<<" v"<<i<<":"<<ve[i];
}
cout<<endl;
*/
}
void Grap::time_2(){
vl[vertexNum] = ve[vertexNum];
for(int i=edgeNum; i>=1; i--){
int a = edge[i].from;
int b = edge[i].to;
int c = vl[b] - edge[i].money;
if(vl[a]==0||c<vl[a])
vl[a] = c;
}
/*
for(int i=1; i<=vertexNum; ++i){
cout<<" v"<<i<<":"<<vl[i];
}
cout<<endl;
*/
}
void Grap::time_3(){
for(int i=1; i<=edgeNum; ++i){
int a = edge[i].from;
ee[i] = ve[a];
}
/*
for(int i=1; i<=edgeNum; ++i){
cout<<" v"<<i<<":"<<ee[i];
}
cout<<endl;
*/
}
void Grap::time_4(){
for(int i=edgeNum; i>=1; i--){
int a = edge[i].from;
int b = edge[i].to;
el[i] = vl[b] - edge[i].money;
}
/*
for(int i=1; i<=edgeNum; ++i){
cout<<" v"<<i<<":"<<el[i];
}
cout<<endl;
*/
}
void Grap::key(){
cout<<"v1 ";
for(int i=1; i<=edgeNum; ++i){
int a = ee[i];
int b = el[i];
if(a==b){
if(i!=1&&a==ee[i-1])
continue;
int x = edge[i].from;
int y = edge[i].to;
cout<<"v"<<y<<" ";
if(y==vertexNum)
break;
}
}
}
int main()
{
int dian,bian;
cin>>dian>>bian;
Grap clz(dian,bian);
}