poj2449 Remmarguts' Date,第K短路,poj2449remmarguts

來源:互聯網
上載者:User

poj2449 Remmarguts' Date,第K短路,poj2449remmarguts

點擊開啟連結


SPFA  + A*


#include <cstdio>#include <queue>#include <cstring>#include <algorithm>using namespace std;struct node {int v, dis, f, next;friend bool operator <(node a, node b){return a.f>b.f;}};const int INF = 1e9;const int maxn = 1005;const int maxm = 100005;node edge[maxm], edgef[maxm];int head[maxn], e, headf[maxn], dis[maxn], n, m, s, t, k;void Add(int u, int v, int dis){edge[e].v = v;edge[e].dis = dis;edge[e].next = head[u];head[u] = e;edgef[e].v = u;edgef[e].dis = dis;edgef[e].next = headf[v];headf[v] = e++;}void init(){int i, u, v, dis;memset(head, -1, sizeof head );memset(headf, -1, sizeof headf );e = 0;for(i=1; i<=m; ++i){scanf("%d%d%d", &u, &v, &dis);Add(u, v, dis);}}int cnt[maxn], vis[maxn];void SPFA(int s, int t){int u, v, i, j;queue<int> q;memset(vis, 0, sizeof vis );memset(cnt, 0, sizeof cnt );for(i=0; i<=n; ++i) dis[i] = INF;vis[s]=1; dis[s]=0; q.push(s);while(!q.empty()){u = q.front();q.pop();vis[u] = 0;cnt[u]++;if(cnt[u]>=n) return ;for(j=headf[u];~j;j=edgef[j].next)if(dis[v=edgef[j].v]>dis[u]+edgef[j].dis){dis[v] = dis[u] + edgef[j].dis;if(!vis[v]) vis[v]=1,q.push(v);}}}int A_STAR(int s, int t, int k){int cnt=0, i;node e, next;priority_queue<node> q;if(s==t) k++;if(dis[s]==INF) return -1;e.v = s; e.dis = 0; e.f = dis[s]; q.push(e);while(!q.empty()){e = q.top();q.pop();if(e.v==t&&++cnt==k) return e.dis;for(i=head[e.v]; ~i; i=edge[i].next){next.v = edge[i].v;next.dis = e.dis + edge[i].dis;next.f = next.dis + dis[next.v];q.push(next);}}return -1;}void solve(){scanf("%d%d%d", &s, &t, &k);SPFA(t, s);printf("%d\n", A_STAR(s, t, k));}int main(){while(~scanf("%d%d", &n, &m)){init();solve();}return 0;}


參考連結:

http://www.cnblogs.com/vongang/archive/2012/07/17/2594737.html

http://yzmduncan.iteye.com/blog/1162759

http://ycool.com/post/krb8pah


poj2449 a*第k短路 助

1000]of boolean;
f,q:array[0..1000000]of longint;
n,m,k,s,e,ans:longint;

procedure scanf;//加邊。這個也應該對
var
i,x,y,z:longint;
begin
readln(n,m);
fillchar(a,sizeof(a),255);
for i:=1 to m do
begin
readln(x,y,z);
inc(b[x,0]); b[x,b[x,0]]:=y;
a[x,y]:=z;
end;
readln(s,e,k);
if s=e then inc(k);
end;

procedure spfa;
//spfa保證肯定是對的,就不用看了,就是把邊倒置,從終點做spfa麼

function best(t:longint):longint;//這個相當於寫堆,就是慢了,樸素的,就是把整個隊列掃一遍找個評估值最優的
var
min,i:longint;
begin
min:=maxlongint;
for i:=1 to t do
if f[i]+dis[q[i]]<min then//f是當前累加的距離和,加上dis就是評估值
begin
min:=f[i]+dis[q[i]];
best:=i;
end;
if min>=maxlongint div 100 then exit(maxlongint);
end;

procedure A_star;
var
t,i,x,now:longint;
begin
fillchar(q,sizeof(q),0);
t:=1; q[1]:=s;
while d[e]<k do
begin
x:=best(t);
if x=maxlongint then break;
now:=q[x];
inc(d[now]);//d數組是記錄找到了第幾短路了
if d[e]=k then begin ans:=f[x]; break; end;
if d[now]<=k then
for i:=1 to b[now,0] do
begin
inc(t);
q[t]:=b[now,i];
f[t]:=f[x]+a[now,b[now,i]];//f數組是當前累加的距離和
end;
f[x]:=maxlongint div 100;//搜過的賦為極大值
end;//while
end;

begin
scanf;
spfa;
A_star;
if d[e]=k then writeln(ans) else writeln(-1);
end.
參考資料:sername...餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.