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 |
Re:汗,丢失了这个细节:both numbers are not greater than 10000 by absolute valueIn Reply To:汗,丢失了这个细节:both numbers are not greater than 10000 by absolute value Posted by:faintxcl at 2009-02-25 15:21:04 为什么改了 邻接表+spfa 就re了呢?求教。。 #include <iostream> #include <algorithm> #include <cmath> #include <queue> using namespace std; #define MAXN 20000 int ans[MAXN]; int n;//顶点数 typedef struct NODE { //int next; struct NODE *next; int value; int v; }node; node *cow[MAXN]; bool mark[MAXN]; int q[MAXN*5];//队列 int maxv; inline void AddEdge(int s, int e,int value) { node *p; p=(NODE*)malloc(sizeof(NODE)); p->value=value; p->v=e; p->next=cow[s]; cow[s]=p; } int Spfa(int s) { int front = 0, rear = 1; fill(ans, ans+maxv, -1); memset(q,sizeof(q),0); memset(mark,false,sizeof(mark)); ans[s] = 0; mark[s] = true; q[front] = s; while (front != rear) { int vertex = q[front++]; mark[vertex] = false; node *itor; for(itor=cow[vertex];itor;itor=itor->next) { int val = ans[vertex]+itor->value; int nextPt = itor->v; if (val > ans[nextPt]) { ans[nextPt] = val; if (!mark[nextPt]) { mark[nextPt] = true; q[rear++] = nextPt; } } } } //for(int i=0; i<maxv; i++)cout<<ans[i]; return ans[maxv]; } int main() { int n, min, i, j, cal, u, k,c,temp; int K; while(scanf("%d %d", &K,&n) != EOF) { maxv = -1; for(i=0; i<MAXN; i++)cow[i]=NULL; for(k = 0; k < n; k++) { scanf("%d%d", &i, &j); i+=10001; j+=10001; if(i>j)temp=i,i=j,j=temp; j++; if(j-i>=K)c=K; else c=j-i; AddEdge(i,j,c); maxv = max(maxv, j); } maxv++; for ( i = 1; i <= maxv; ++i) { AddEdge(i-1,i,0); AddEdge(i,i-1,-1); } int anss=Spfa(0); printf("%d\n", anss); for(int i=0; i<=maxv; i++) if(ans[i+1]-ans[i]==1) printf("%d\n",i-10001); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator