ACM求解4

来源:百度知道 编辑:UC知道 时间:2024/05/27 23:37:03
Description

Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company

is uncooperative, so he needs to pay for some of the cables required to connect his farm to

the phone system.

There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are

scattered around Farmer John's property; no cables connect any them. A total of P (1 ≤ P ≤

10,000) pairs of poles can be connected by a cable; the rest are too far apart.

The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤

1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once.

Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N

need to be connected by a path of cables; the rest of the poles might be used or might not

be used.

As it turns out, the phon

这个网上都可以找到的:
核心代码如下:
int dijk()
{
memset(dps,-1,sizeof(dps));
priority_queue<elem> qu;
qu.push(elem(0,0,0));
int ax,ak,al,bx,bl,trvl;
while (!qu.empty())
{
elem tmp=qu.top();
qu.pop();
ax=tmp.py;
ak=tmp.pk;
al=tmp.pl;
if (ax==n-1) return al;
if (dps[ax][ak]<0)
{
dps[ax][ak]=al;
trvl=nodes[ax];
while (trvl>-1)
{
bx=sts[trvl].py;
bl=sts[trvl].len;
trvl=sts[trvl].next;
if (dps[bx][ak]<0) qu.push(elem(bx,ak,smax(al,bl)));
if (ak<k&&dps[bx][ak+1]<0) qu.push(elem(bx,ak+1,al));
}
}
}
return -1;
}