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:有人用一次迪杰斯特拉不枚举,通过控制中间选路过程进行等级限制,AC的吗?In Reply To:有人用一次迪杰斯特拉不枚举,通过控制中间选路过程进行等级限制,AC的吗? Posted by:Matrixking at 2012-05-10 14:00:30 我的不枚举代码虽然AC了,但是理论上还是能找出反例。主要的思路是纪录每次中间路径的最大、最小rank,其差值不能大于M。用本节点和上个节点共同控制访问,防止同一路径走两次,但是这个思路还是有bug。如果不做访问控制,则相同的值循环访问会造成死循环。最大的难点在于怎么控制同样的路径不能走两次(路径不能有环),附上我的代码,具体怎么做还没想好 #include <stdio.h> #include <stdlib.h> #define MAX_N 100 int M, N; struct item { int p; /* price */ int r; /* rank */ int so[MAX_N]; /* swap ordinal */ int sp[MAX_N]; /* swap price */ int sn; /* swap count */ } items[MAX_N + 1]; int v[MAX_N + 1][MAX_N + 1]; struct q_item { int p; int o; int fo; int min_r; int max_r; } q[MAX_N * MAX_N + 1]; int q_size; void percolate_up(int i) { struct q_item tmp; if(i == 1) return; if(q[i].p < q[i / 2].p) { tmp = q[i]; q[i] = q[i / 2]; q[i / 2] = tmp; percolate_up(i / 2); } } void percolate_down(int r, int s) { struct q_item tmp; int small = r; if(r * 2 <= s && q[r * 2].p < q[small].p) small = r * 2; if( r * 2 + 1 <= s && q[r * 2 + 1].p < q[small].p) small = r * 2 + 1; if(small != r) { tmp = q[r]; q[r] = q[small]; q[small] = tmp; percolate_down(small, s); } } void add_to_heap(struct q_item qi) { q[++q_size] = qi; percolate_up(q_size); //printf("%d %d %d %d added to heap\n", qi.p, qi.o, qi.min_r, qi.max_r); } struct q_item heap_pop() { struct q_item qi; qi = q[1]; q[1] = q[q_size--]; percolate_down(1, q_size); return qi; } int main(int argc, char *argv[]) { int i, sn, so, r; struct q_item qi, qi2; scanf("%d %d", &M, &N); for(i = 1; i <= N; ++i) { scanf("%d %d %d", &items[i].p, &items[i].r, &sn); while(sn--) { scanf("%d", &so); items[so].so[items[so].sn] = i; scanf("%d", &items[so].sp[items[so].sn++]); } } for(i = 1; i <= N; ++i) { if(abs(items[i].r - items[1].r) <= M) { qi.p = items[i].p; qi.o = i; qi.fo = i; qi.min_r = qi.max_r = items[i].r; add_to_heap(qi); } } while(1) { qi = heap_pop(); if(qi.o == 1) { printf("%d\n", qi.p); break; } if(v[qi.o][qi.fo]) continue; v[qi.o][qi.fo] = 1; for(i = 0; i < items[qi.o].sn; ++i) { qi2.p = qi.p + items[qi.o].sp[i]; qi2.o = items[qi.o].so[i]; qi2.fo = qi.o; qi2.min_r = qi.min_r; qi2.max_r = qi.max_r; r = items[qi2.o].r; if(r < qi2.min_r) qi2.min_r = r; if(r > qi2.max_r) qi2.max_r = r; if(qi2.max_r - qi2.min_r <= M) add_to_heap(qi2); } } exit(0); } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator