标签:string simple queue sig cst ini struct amp max
http://acm.hdu.edu.cn/showproblem.php?pid=4109
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2196 Accepted Submission(s): 900
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 const int maxn = 1e3 + 5; 9 struct node 10 { 11 int to, w; 12 node(){} 13 node(int tt, int ww) : to(tt), w(ww){} 14 }; 15 vector<node> v[maxn]; 16 int e[maxn], deg[maxn], n, m, x, y, z; 17 void TOP() 18 { 19 queue<int> q; 20 for(int i = 0; i < n; i++) 21 if(!deg[i]) 22 q.push(i), e[i] = 1; 23 while(!q.empty()) 24 { 25 int u = q.front(); 26 q.pop(); 27 for(int i = 0; i < v[u].size(); i++) 28 { 29 int to = v[u][i].to, w = v[u][i].w; 30 if(e[to] < e[u]+w) 31 e[to] = e[u]+w; 32 if(--deg[to] == 0) 33 q.push(to); 34 } 35 } 36 } 37 int main() 38 { 39 while(~scanf("%d%d", &n, &m)) 40 { 41 memset(deg, 0, sizeof(deg)); 42 memset(e, 0, sizeof(e)); 43 for(int i = 0; i < maxn; i++) 44 v[i].clear(); 45 for(int i = 1; i <= m; i++) 46 { 47 scanf("%d%d%d", &x, &y, &z); 48 v[x].push_back(node(y, z)); 49 deg[y]++; 50 } 51 TOP(); 52 int ans = 0; 53 for(int i = 0; i < n; i++) 54 ans = max(ans, e[i]); 55 printf("%d\n", ans); 56 } 57 return 0; 58 }
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 struct edge{ 7 int to; 8 int len; 9 int next; 10 }qwq[11150]; 11 queue<int>pa; 12 int edge_cnt,head[1005],stk[1005],dist[1005]; 13 void add(int x,int y,int z) 14 { 15 qwq[edge_cnt].to=y; 16 qwq[edge_cnt].len=z; 17 qwq[edge_cnt].next=head[x]; 18 head[x]=edge_cnt++; 19 } 20 void spfa() 21 { 22 while(!pa.empty()) 23 { 24 pa.pop(); 25 } 26 pa.push(0); 27 stk[0]=1; 28 while(!pa.empty()) 29 { 30 int u=pa.front();pa.pop();stk[u]=0; 31 for(int i = head[u]; i != -1 ; i=qwq[i].next) 32 { 33 int v=qwq[i].to; 34 int llen=qwq[i].len; 35 if(dist[v]<llen+dist[u]) 36 { 37 dist[v]=llen+dist[u]; 38 if(!stk[v]) 39 { 40 stk[v]=1; 41 pa.push(v); 42 } 43 } 44 } 45 } 46 } 47 int main() 48 { 49 int n,m; 50 while(scanf("%d%d",&n,&m)==2) 51 { 52 memset(head,-1,sizeof(head)); 53 memset(dist,-1,sizeof(dist)); 54 memset(stk,0,sizeof(stk)); 55 dist[0]=0; 56 edge_cnt=0; 57 while(m--) 58 { 59 int a,b,c; 60 scanf("%d%d%d",&a,&b,&c); 61 add(a,b,c); 62 } 63 for(int i = 1 ; i <= n ;i++) 64 { 65 add(0,i,0); 66 } 67 spfa(); 68 int maxx=-1; 69 for(int i = 0 ; i <= n ; i++) 70 { 71 if(dist[i]>maxx) 72 { 73 maxx=dist[i]; 74 } 75 } 76 cout << maxx+1 << endl; 77 } 78 return 0; 79 }
标签:string simple queue sig cst ini struct amp max
原文地址:https://www.cnblogs.com/MekakuCityActor/p/9031366.html