用bellman_ford的方法,将中间不断取较小值,修改为取较大值就可以了
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 const int N = 105; 6 int dp[N] , k , first[N] , val[N] , x , y , fa[N] , rec[N]; 7 8 struct Edge{ 9 int x , y , next , d; 10 }e[N*N]; 11 12 void add_edge(int x , int y , int d) 13 { 14 e[k].x = x , e[k].y = y , e[k].next = first[x] , e[k].d = d; 15 first[x] = k++; 16 } 17 18 int main() 19 { 20 int T , n , cas = 0; 21 scanf("%d" , &T); 22 while(T--){ 23 scanf("%d" , &n); 24 for(int i = 1 ; i<=n ; i++) 25 scanf("%d" , val+i); 26 val[n+1] = 0; 27 28 int m; 29 scanf("%d" , &m); 30 k=0; 31 for(int i = 0 ; i<m ; i++){ 32 scanf("%d%d" , &x , &y); 33 add_edge(x , y , val[y]); 34 } 35 36 memset(dp , 0 , sizeof(dp)); 37 memset(fa , 0 , sizeof(fa)); 38 for(int i = 1 ; i<=n ; i++){ 39 for(int j = 0 ; j<k ; j++){ 40 int u = e[j].x , v = e[j].y; 41 if(dp[v] < dp[u] + e[j].d){ 42 dp[v] = dp[u] + e[j].d; 43 fa[v] = u; 44 // cout<<"here: "<<u<<" "<<v<<" "<<fa[v]<<" "<<dp[v]<<endl; 45 } 46 } 47 } 48 49 printf("CASE %d#\npoints : %d\ncircuit : " , ++cas , dp[n+1]); 50 int cnt = 0 , la = n+1; 51 rec[cnt++] = 1; 52 while(fa[la]){ 53 rec[cnt++] = fa[la]; 54 la = fa[la]; 55 } 56 for(int i = cnt-1 ; i>=1 ; i--) 57 printf("%d->" , rec[i]); 58 printf("%d\n" , rec[0]); 59 if(T>0) puts(""); 60 } 61 return 0; 62 }
我还在坚持,我还未达到我所想,梦~~一直在