乙個看錯題面帶來的問題 最小標號最短路

2021-07-15 03:02:05 字數 2145 閱讀 4384

有乙個問題我看錯了題面,不會做,問anque,卻發現看錯的題意卻有精妙做法。

題意簡述:有一幅n個點,m條邊的有向圖(2<=n<=100000,1<=m<=200000),求1到n最短路中標號最小的路徑。

首先,做s點的最短路,如果dis(x)+d(x,y)=dis(y),則邊(x,y)可能在s點出發的最短路上。

這題精妙之處在於如果求出了源點的單源最短路不能保證是標號最小的路徑,如果挑可能在最短路上的邊走,不一定能走到匯點。

精妙做法就是把邊反向,做匯點的單元最短路,從源點走可能在匯點最短路上的邊,並且在走的時候,時刻選標號最小的點。因為在源點走可能在匯點最短路上的邊必然走的是到匯點的最短路,這時候挑標號最小的點是沒有問題的;如果做了源點的最短路,不能保證走可能在源點最短路上的邊能走到匯點。

const 

mxn=100001;

inf=maxlongint>>1;

type

point=record

y,v,next:longint;

end;

var map:array[0..500000] of point;

first,dis,q,a:array[0..mxn] of longint;

inque:array[0..mxn] of boolean;

n,m,s,x,y,v,head,tail,t,i:longint;

procedure

ins(x,y,v:longint);

begin

inc(s);

map[s].y:=y;

map[s].v:=v;

map[s].next:=first[x];

first[x]:=s;

end;

begin

fillchar(first,sizeof(first),0);s:=1;

readln(n,m);

for i:=1

to m do

begin

read(x,y,v);

ins(x,y,v);

ins(y,x,v);

end;

for i:=1

to n do dis[i]:=inf;

fillchar(inque,sizeof(inque),false);

head:=1;tail:=2;

q[head]:=n;dis[n]:=0;inque[n]:=true;

while head<>tail do

begin

x:=q[head];

t:=first[x];

while t>0

dobegin

y:=map[t].y;

if (t and

1=1)and(dis[x]+map[t].vthen

begin

dis[y]:=dis[x]+map[t].v;

ifnot inque[y] then

begin

inque[y]:=true;

q[tail]:=y;

inc(tail);if tail>mxn then tail:=1;

end;

end;

t:=map[t].next;

end;

inque[x]:=false;

inc(head);if head>mxn then head:=1;

end;

s:=1;

a[s]:=1;

while a[s]<>n do

begin

x:=a[s];

t:=first[x];

inc(s);a[s]:=inf;

while t>0

dobegin

if (t and

1=0)and(dis[x]-map[t].v=dis[map[t].y])and(map[y].ythen a[s]:=map[t].y;

t:=map[t].next;

end;

end;

writeln(dis[1]);

for i:=1

to s-1

dowrite(a[i],' ');

writeln(a[s]);

end.

乙個問題帶來的學習

故事 從前,有乙個地主,他擁有很多座糧倉,分別儲存了不同單位的糧食,分布在該城鎮的不同地點,短時間內也難以再操作。有一天,山大王殺到他家裡,要挾道 我知道你有好幾座糧倉,現在我正在招兵買馬,糧草不足,您老救濟一下吧,交出幾座糧倉,保您老身體健康,發財大吉啊!要過這個冬天,弟兄們算了下需要30單位的糧...

css巢狀帶來的乙個問題

頁面html如下 contents 不幸的是,上述html 所在的頁面引入了乙個外部css檔案,其中對span做了如下樣式定義 這種一刀切的方式很不好 span 這就造成了html 無法正確反映在頁面上。因為第乙個span的樣式 雖然不受外部css檔案中樣式定義的影響,因為如果某些屬性在不同的樣式表...

protected訪問標號的乙個生僻

c primer 15.2.2節中有這樣一段話 派生類只能通過派生類物件訪問其基類的protected成員,派生類對其基類型別物件的protected成員沒有特殊許可權。例子 item base是bulk item的基類,bulk item定義了乙個成員方法,接受乙個bulk item物件的引用,和...