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

题目大意

  略。

分析

  注意!!!,此题只有一个源点,但有多个汇点
  这题本质上是求源点到汇点字典序的带权最长路,从前往后求比较麻烦,我们可以考虑从后往前求,求每个节点到某个汇点的最长距离和字典序最小的后继节点。
  为此可先求拓扑序列,然后从后往前遍历拓扑序列,并进行松弛操作(同时也可以解决多汇点问题,SPFA则不行)。

代码如下

 #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 ;
}

最新文章

  1. RxJava 备注
  2. Android配置----Eclipse+BlueStacks调试Android应用
  3. Tomcat配置虚拟主机后的登录验证码问题
  4. HDU 2897 邂逅明下 (简单博弈,找规律)
  5. linux tail
  6. sicily 4378 connected components in undirected graph
  7. php回滚
  8. 微信小程序-开发入门
  9. Netty入门
  10. JDBC url连接字符串错误1
  11. 【Linux基础】Linux更改系统IP
  12. Python学习笔记-随机数
  13. 【链接】linuxCentOS权限问题修复(chmod777-R或者chmod755- http://www.cnblogs.com/kofxxf/p/5220836.html
  14. 3.2 git命令大全
  15. leetcode-algorithms-30 Substring with Concatenation of All Words
  16. tomcat配置的环境变量catalina.home和catalina.base 区别
  17. 第一个 MVC 应用程序(下半部分)
  18. PyQt4(简单布局)
  19. 开篇~试试word写博客
  20. [SVN技巧]代码提交中遇到的两个问题及其解决方案

热门文章

  1. 个人笔记 - MATLAB
  2. HTML-参考手册: HTML 事件属性
  3. Qt5.2中使用ping命令实现Ip扫描功能
  4. 都 9102 年了,一起来攻克 Linux 吧!
  5. Java7任务并行执行神器:Fork&Join框架
  6. Arcpy 遍历每一个要素新建要素类
  7. C stdarg.h
  8. vector 有点麻烦啊 能简单点么?
  9. 【LeetCode】贪心
  10. python_django_celery的初步使用