1 #include <stdlib.h> 2 #include <cstdio> 3 #include <queue> 4 #define MaxVertexNum 102 5 #define INFINITY 65536 6 using namespace std; 7 8 typedef int Vertex; 9 typedef int WeightType; 10 typedef int DataType; 11 12 int Indegree[MaxVertexNum]; 13 14 15 typedef struct ENode *Edge; 16 struct ENode{ 17 Vertex V1, V2; 18 WeightType Weight; 19 }; 20 21 typedef struct AdjVNode *PtrToAdjVNode; 22 struct AdjVNode{ 23 Vertex AdjV; 24 WeightType Weight; 25 PtrToAdjVNode Next; 26 }; 27 28 typedef struct Vnode{ 29 PtrToAdjVNode FirstEdge; 30 DataType Data; 31 } AdjList[MaxVertexNum]; /* AdjListÊÇÁÚ½Ó±íÀàÐÍ */ 32 33 34 35 typedef struct GNode *LGraph; 36 struct GNode{ 37 int Nv; 38 int Ne; 39 AdjList G; 40 }; 41 42 43 44 LGraph CreateGraph( int VertexNum ); 45 void InsertEdge( LGraph Graph, Edge E ); 46 LGraph BuildGraph(); 47 bool topSort(LGraph Graph); 48 49 void find_max(LGraph Graph); 50 51 int main() { 52 LGraph Graph = BuildGraph(); 53 54 if( !topSort(Graph) ) printf("Impossible\n"); 55 else { 56 find_max(Graph); 57 } 58 59 60 } 61 62 LGraph CreateGraph( int VertexNum ) 63 { 64 Vertex V; 65 LGraph Graph; 66 67 Graph = (LGraph)malloc( sizeof(struct GNode) ); 68 Graph->Nv = VertexNum; 69 Graph->Ne = 0; 70 71 for (V=0; V<Graph->Nv; V++) 72 Graph->G[V].FirstEdge = NULL; 73 74 return Graph; 75 } 76 void InsertEdge( LGraph Graph, Edge E ) 77 { 78 PtrToAdjVNode newNode = new AdjVNode; 79 newNode->Weight = E->Weight; 80 newNode->AdjV = E->V2; 81 82 newNode->Next = Graph->G[E->V1].FirstEdge; 83 Graph->G[E->V1].FirstEdge = newNode; 84 85 86 } 87 LGraph BuildGraph() 88 { 89 LGraph Graph; 90 int Nv; 91 Edge E; 92 Vertex V; 93 94 scanf("%d", &Nv); 95 Graph = CreateGraph(Nv); 96 97 scanf("%d", &(Graph->Ne)); 98 99 if(Graph->Ne) { 100 E = new ENode; 101 for(int i=0; i<Graph->Ne; i++) { 102 scanf("%d %d %d", &(E->V1), &(E->V2), &(E->Weight)); 103 InsertEdge(Graph, E); 104 } 105 } 106 107 for(V=0; V<Graph->Nv; V++) { 108 Graph->G[V].Data = 0; 109 } 110 return Graph; 111 112 } 113 bool topSort(LGraph Graph) 114 { 115 int cnt; 116 Vertex V; 117 queue<Vertex> Q; 118 119 cnt = 0; 120 for( V=0; V<Graph->Nv; V++) Indegree[V] = 0; 121 122 for( V = 0; V < Graph->Nv; V++) { 123 for(PtrToAdjVNode W = Graph->G[V].FirstEdge; W; W=W->Next) { 124 Indegree[W->AdjV]++; 125 Graph->G[W->AdjV].Data = 0; 126 } 127 } 128 for(V=0; V<Graph->Nv; V++) if(Indegree[V] == 0) Q.push(V); 129 130 while(!Q.empty()) { 131 V = Q.front(); 132 Q.pop(); 133 cnt++; 134 135 for(PtrToAdjVNode W = Graph->G[V].FirstEdge; W; W=W->Next) { 136 if(--Indegree[W->AdjV] == 0) Q.push(W->AdjV); 137 if(Graph->G[W->AdjV].Data < W->Weight + Graph->G[V].Data) 138 Graph->G[W->AdjV].Data = W->Weight + Graph->G[V].Data; 139 140 } 141 142 143 } 144 145 if(cnt != Graph->Nv) return false; 146 return true; 147 148 149 } 150 151 void find_max(LGraph Graph) 152 { 153 Vertex V; 154 int compTime; 155 compTime = Graph->G[0].Data; 156 157 for(V = 1; V < Graph->Nv; V++) { 158 if(Graph->G[V].Data > compTime) compTime = Graph->G[V].Data; 159 } 160 printf("%d\n", compTime); 161 }