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 |
反向建图+SPFA可以AC0.0#include<stdio.h> #include<string.h> #include<queue> using namespace std; int head[200005]; struct EdgeNode { int to; int w; int next; }e[200005*20]; int degree[200005]; int val[200005]; int vis[200005]; int dis[200005]; int n,m; int cont; void add(int from,int to) { e[cont].to=to; e[cont].next=head[from]; head[from]=cont++; } void SPFA() { queue<int >s; for(int i=0;i<n;i++)dis[i]=-0x3f3f3f3f; for(int i=0;i<n;i++) { if(degree[i]==0) { dis[i]=val[i]; vis[i]=1; s.push(i); } } while(!s.empty()) { int u=s.front(); s.pop();vis[u]=0; for(int k=head[u];k!=-1;k=e[k].next) { int v=e[k].to; if(dis[v]<dis[u]+val[v]) { dis[v]=dis[u]+val[v]; if(vis[v]==0) { vis[v]=1; s.push(v); } } } } int output=-0x3f3f3f3f; for(int i=0;i<n;i++) { if(head[i]==-1) output=max(output,dis[i]); } printf("%d\n",output); } int main() { while(~scanf("%d%d",&n,&m)) { cont=0; memset(vis,0,sizeof(vis)); memset(degree,0,sizeof(degree)); memset(head,-1,sizeof(head)); for(int i=0;i<n;i++) { scanf("%d",&val[i]); } for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); x--;y--; add(y,x); degree[x]++; } SPFA(); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator