标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1715 Accepted Submission(s): 757
Special Judge
#include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <algorithm> #include <math.h> using namespace std; typedef long long LL; const int INF = 999999999; const int N = 1000; struct Edge{ int v,w,next; }edge[10005]; int head[N]; int n,tot; int val[N]; void init(){ memset(head,-1,sizeof(head)); tot = 0; } void addEdge(int u,int v,int w,int &k){ edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u] = k++; } int low[N],time[N]; bool vis[N]; int spfa(int s){ for(int i=0;i<=n;i++){ vis[i] = false; low[i] = -INF; time[i] = 0; } low[s] = 0; time[s]++; queue<int> q; q.push(s); int num = ((int)sqrt(n)+1); ///改成根号 n 可以AC... while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = false; for(int k=head[u];k!=-1;k=edge[k].next){ int v = edge[k].v,w=edge[k].w; if(low[v]<low[u]+w){ low[v] = low[u]+w; if(!vis[v]){ vis[v] = true; q.push(v); if(time[v]++>num) return 0; } } } } return 1; } int main(){ int t = 1; while(scanf("%d",&n)!=EOF,n){ init(); int MAX = -1; for(int i=1;i<=n;i++){ scanf("%d",&val[i]); } char str[10]; int super = 0; while(scanf("%s",str)){ if(strcmp(str,"#")==0) break; int a,b; scanf("%d%d",&a,&b); if(strcmp(str,"SAF")==0){ addEdge(b,a,val[b],tot); }else if(strcmp(str,"FAF")==0){ addEdge(b,a,-(val[a]-val[b]),tot); }else if(strcmp(str,"FAS")==0){ addEdge(b,a,-val[a],tot); }else{ addEdge(b,a,0,tot); } } for(int i=1;i<=n;i++){ addEdge(super,i,0,tot); } printf("Case %d:\n",t++); if(spfa(super)){ for(int i=1;i<=n;i++){ printf("%d %d\n",i,low[i]); } }else{ printf("impossible\n"); } printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5698949.html