标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8016 Accepted Submission(s): 2092
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<math.h> using namespace std; const double INF = 999999999; const int N = 805; const int M = 800000; struct Edge{ int v,next; double w; }edge[M]; int head[N],tot; int n,m,l,r; void init(){ memset(head,-1,sizeof(head)); tot = 0; } void addEdge(int u,int v,double w,int &k){ edge[k].v =v ,edge[k].w = w ,edge[k].next = head[u],head[u] = k++; } double low[N]; int time[N]; bool vis[N]; bool spfa(int s){ for(int i=0;i<=n+m;i++){ vis[i] = false; time[i] = 0; low[i] = INF; } int num = ((int)sqrt(n+m))+1; low[s] = 0; time[s]++; queue<int>q; q.push(s); 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; double w = edge[k].w; if(low[v]>low[u]+w){ low[v] = low[u]+w; if(!vis[v]){ vis[v] = true; q.push(v); time[v]++; if(time[v]>num) return false; } } } } return true; } int main() { double c; while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF){ init(); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%lf",&c); addEdge(i,n+j,-log(l/c),tot); addEdge(n+j,i,log(r/c),tot); } } for(int i=1;i<=n+m;i++){ addEdge(0,i,0,tot); } if(spfa(0)) printf("YES\n"); else printf("NO\n"); } return 0; }
手动栈解决:
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<math.h> using namespace std; const double INF = 999999999; const int N = 805; const int M = 800000; struct Edge{ int v,next; double w; }edge[M]; int head[N],tot; int n,m,l,r; void init(){ memset(head,-1,sizeof(head)); tot = 0; } void addEdge(int u,int v,double w,int &k){ edge[k].v =v ,edge[k].w = w ,edge[k].next = head[u],head[u] = k++; } double low[N]; int time[N]; bool vis[N]; int stk[N*N]; bool spfa(int s){ for(int i=0;i<=n+m;i++){ vis[i] = false; time[i] = 0; low[i] = INF; } int top = 0; low[s] = 0; time[s]++; stk[top++] = s; while(top!=0){ int u = stk[--top]; vis[u] = false; for(int k=head[u];k!=-1;k=edge[k].next){ int v = edge[k].v; double w = edge[k].w; if(low[v]>low[u]+w){ low[v] = low[u]+w; if(!vis[v]){ vis[v] = true; stk[top++] = v; time[v]++; if(time[v]>n+m) return false; } } } } return true; } int main() { double c; while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF){ init(); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%lf",&c); addEdge(i,n+j,-log(l/c),tot); addEdge(n+j,i,log(r/c),tot); } } for(int i=1;i<=n+m;i++){ addEdge(0,i,0,tot); } if(spfa(0)) printf("YES\n"); else printf("NO\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5699125.html