#include #include #include using namespace std; typedef long long Int; struct grph { Int ver,cost; }; vector Graph[250001]; Int magictable[501][501]; Int n,m,a,b; Int rx,ry; ///Djikstra Int D[250001]; bool TFO[250001]; grph Heap[1000001]; Int whereis[1000001]; Int TwoToOne(Int x,Int y) { Int num=(x-1)*m + y; return num; } void OneToTwo(Int num) { ry=num%m; if (ry==0) ry=m; rx=(num-ry)/m + 1; return; } void Update(Int hind) { Int dad,son=hind; grph d; Int in; dad=son/2; while(dad>0) { if (Heap[dad].cost>Heap[son].cost) { whereis[ Heap[dad].ver ]=son; whereis[ Heap[son].ver ]=dad; d=Heap[dad]; Heap[dad]=Heap[son]; Heap[son]=d; son=dad; } else { break; } dad=son/2; } return; } grph RemoveTop() { grph toreturn=Heap[1]; Int dad=1,son1,son2; grph d; Heap[1].cost=999999999999; while(dad<500000) { son1=dad*2; son2=dad*2+1; if (Heap[son1].cost=1 && nextx<=n && nexty>=1 && nexty<=m) { nextver=TwoToOne(nextx,nexty); help.ver=nextver; help.cost=b; if (magictable[i][j]!=magictable[nextx][nexty]) { help.cost=help.cost+a; } Graph[curver].push_back(help); } nextx=i; nexty=j+1; if (nextx>=1 && nextx<=n && nexty>=1 && nexty<=m) { nextver=TwoToOne(nextx,nexty); help.ver=nextver; help.cost=b; if (magictable[i][j]!=magictable[nextx][nexty]) { help.cost=help.cost+a; } Graph[curver].push_back(help); } nextx=i-1; nexty=j; if (nextx>=1 && nextx<=n && nexty>=1 && nexty<=m) { nextver=TwoToOne(nextx,nexty); help.ver=nextver; help.cost=b; if (magictable[i][j]!=magictable[nextx][nexty]) { help.cost=help.cost+a; } Graph[curver].push_back(help); } nextx=i; nexty=j-1; if (nextx>=1 && nextx<=n && nexty>=1 && nexty<=m) { nextver=TwoToOne(nextx,nexty); help.ver=nextver; help.cost=b; if (magictable[i][j]!=magictable[nextx][nexty]) { help.cost=help.cost+a; } Graph[curver].push_back(help); } } } printf("%I64d\n",Djikstra(s,e)); return 0; }