Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
求教。。一直tle mle ...#include<iostream> #include<queue> #define inf -2100000000 #define N 100001 //using namespace std; struct Node{ int v; Node *next; Node(){next=NULL;} }*node[N]; int n,m; int value[N]; int in[N]; int out[N]; //queue<int >Q; int Q[N]; void AddEdge(int s,int e) { Node *p; p=(Node *)malloc(sizeof(Node)); p->v=e; p->next=node[s]; node[s]=p; } int dp[N]; void TopSort(){ memset(Q,0,sizeof(Q)); int front=0,tail=0; for(int i=1; i<=n; i++) if(in[i]==0){ //Q.push(i); Q[tail++]=i; dp[i]=value[i]; } while(front!=tail) { int t=Q[front++]; Node *adj; for(adj=node[t];adj!=NULL;adj=adj->next) { if(--in[adj->v]==0)Q[tail++]=adj->v; if(dp[t]+value[adj->v]>dp[adj->v]) dp[adj->v]=dp[t]+value[adj->v]; } } } int main(){ int Max; while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1; i<=n; i++){ node[i]=NULL; dp[i]=inf; } for(int i=1; i<=n; i++){ scanf("%d",&value[i]); } int s,e; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i =0; i<m; i++) { scanf("%d %d",&s,&e); AddEdge(s,e); in[e]++; out[s]++; } TopSort(); Max=inf; for(int i=1; i<=n; i++) { if(out[i]==0&&Max<dp[i])Max=dp[i]; } printf("%d\n",Max); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator