/****************************************************************************** * Sample solution for the Quantum problem in Progsm'02 * Author: Andreas Björklund ******************************************************************************/ #include #define MAX_SIZE ((1<<20)+1) #define HUGE_COST (1<<31-1) #define NO_PRIO (-1) #define MAX_OPER (32) #define DEBUG (0) #if DEBUG #define ASSERT(x) if(!(x)) {printf("Assertion failed at %d.\n",__LINE__);exit(1);} #define HEAPSANITY(x) heapSanity(x) #else #define ASSERT(x) #define HEAPSANITY(x) #endif struct tOper { int cost; int mask; int flip; int set; }; struct tMxElem { int cost; int prio; }; struct tPrio { int cost; int who; }; struct tMxElem Mx[MAX_SIZE]; struct tPrio Heap[MAX_SIZE]; struct tOper op[MAX_OPER]; int nHeap; int nop,nw,l; void clearMx(int mag) { int i; for (i=0;i<(1<Heap[choice+1].cost) choice++; if (Heap[choice].cost1) { choice = who>>1; if (Heap[choice].cost>Heap[who].cost) { dummy=Heap[choice]; Heap[choice]=Heap[who]; Heap[who]=dummy; Mx[Heap[who].who].prio=who; Mx[Heap[choice].who].prio=choice; who=choice; } else break; } return who; }; int heapInsert(int who,int cost) { nHeap++; Heap[nHeap].who = who; Heap[nHeap].cost = cost; who = heapBubbleUp(nHeap); HEAPSANITY("heapInsert"); return who; }; int heapDecrease(int who, int cost) { if (Heap[who].cost>cost) { Heap[who].cost = cost; who = heapBubbleUp(who); HEAPSANITY("heapDecrease"); return who; } else return who; }; int heapExtractMin(struct tPrio *pPrio) { if (nHeap>0) { *pPrio = Heap[1]; Heap[1]=Heap[nHeap--]; if (nHeap>0) { Mx[Heap[1].who].prio=1; heapBubbleDown(1); } HEAPSANITY("heapExtractMin"); return 1; } else return 0; }; void heapSanity(char *pStr) { int i=2,j; while (i<=nHeap) { if (Heap[i].cost>1].cost) { printf("Heap inconsistent after %s: Size=%d!\n",pStr,nHeap); exit(1); } i++; } } int solveMx(int source,int dest) { int i,which,ncost; struct tPrio myPrio; myPrio.cost = 0; myPrio.who = source; Mx[source].cost= 0; do { Mx[myPrio.who].prio=NO_PRIO; ASSERT(Mx[myPrio.who].cost == myPrio.cost); for (i=0;incost) { ASSERT(!(Mx[which].prio==NO_PRIO && Mx[which].cost!=HUGE_COST)); Mx[which].cost = ncost; if (Mx[which].prio==NO_PRIO) { Mx[which].prio = heapInsert(which,ncost); ASSERT(Heap[Mx[which].prio].who==which); } else { ASSERT(Heap[Mx[which].prio].who==which); Mx[which].prio = heapDecrease(Mx[which].prio,ncost); ASSERT(Heap[Mx[which].prio].who==which); } } } } while (heapExtractMin(&myPrio) && myPrio.who!=dest); return Mx[dest].cost; } int main() { int n,i,j,k,flip,mask,set,cost,dest,src; char ch; scanf("%d",&n); for (i=0;i