It is said to be the DP of the DAG and force it into graph theory. With SPFA, the relaxation operation is changed to be longer. Note the representation of the state.
#include <bits/stdc++.h>using namespacestd;#defineMP Make_pair#defineFi first#defineSe Secondtypedef pair<int,int>PII;Const intN = the;intc[n][3];intd[n][3];BOOLvis[n][3];inttrans[3][2];intMain () {//freopen ("In.txt", "R", stdin); for(inti =0; I <3; i++){ intt =0; for(intj =0; J <3; J + +)if(i!=j) {trans[i][t++] =J; } } intKas =0, N; while(SCANF ("%d",&N), N) { for(inti =0; I < n; i++) {scanf ("%d%d%d", d[i],d[i]+1, d[i]+2); Sort (D[i],d[i]+3); memcpy (C[i],d[i],sizeof(int)*3); } Queue<pii>Q; for(inti =0; I < n; i++){ for(intj =0; J <3; J + +) {Q.push (MP (i,j)); Vis[i][j]=true; } } intHei =0; while(Q.size ()) {PII&u =Q.front (); intid = u.fi,k =u.se; VIS[ID][K]=false; Hei=Max (hei,d[id][k]); intH = c[id][trans[k][0]], W = c[id][trans[k][1]]; if(h>W) Swap (H,W); for(inti =0; I < n; i++){ for(intj =0; J <3; J + +){ intH = c[i][trans[j][0]], W = c[i][trans[j][1]]; if(h>W) swap (H,W); if(HD[id][k]) {D[i][j]= c[i][j]+D[id][k]; if(!Vis[i][j]) {Q.push (MP (i,j)); Vis[i][j]=true; }}}} Q.pop (); } printf ("Case %d:maximum height =%d\n",++Kas,hei); } return 0;}
UVA427 the Tower of Babylon Babel