麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

poj 2449 Remmarguts' Date(K短路)

2019-11-14 08:45:12
字體:
來源:轉載
供稿:網友
題目地址:http://poj.org/PRoblem?id=2449

思路:K短路模板,A*算法。估價函數=當前值+當前位置到終點的距離,即f(p)=g(p)+h(p),每次擴展估價函數值最小的一個。選擇f(p)最小的點,若其為t,則計算其出隊次數,次數等于k時,即為第k短路長度。

#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn=1e3+50;const int maxm=1e5+50;const int INF=0x3f3f3f3f;struct VNode{    int to,next,w;};struct Node{    int to,f,g;    bool Operator <(const Node &rhs) const    {        if(rhs.f==f) return rhs.g<g;        else return rhs.f<f;    }};int S,T,K;int dist[maxn];int tot1,tot2,n,m;int head1[maxm],head2[maxm];VNode edge1[maxm],edge2[maxm];void addEdge1(int u,int v,int w){    edge1[tot1].to=v,edge1[tot1].w=w;    edge1[tot1].next=head1[u],head1[u]=tot1++;}void addEdge2(int u,int v,int w){    edge2[tot2].to=v,edge2[tot2].w=w;    edge2[tot2].next=head2[u],head2[u]=tot2++;}void SPFA(int s,int head[],VNode e[]){    int v[maxn];    queue<int> q;    memset(v,0,sizeof(v));    for(int i=1; i<=n; i++) dist[i]=INF;    v[s]=1,dist[s]=0,q.push(s);    while(!q.empty())    {        int now=q.front();        q.pop(),v[now]=0;        for(int i=head[now]; ~i; i=e[i].next)        {            int nt=e[i].to;            if(dist[nt]>dist[now]+e[i].w)            {                dist[nt]=dist[now]+e[i].w;                if(!v[nt])                {                    v[nt]=1;                    q.push(nt);                }            }        }    }}int A_Star(int s,int t,int k,int head[],VNode e[]){    int cnt=0;    if(s==t) k++;    if(dist[s]==INF) return -1;    Node now;    now.to=s,now.g=0,now.f=now.g+dist[s];    priority_queue<Node> pq;    pq.push(now);    while(!pq.empty())    {        Node now=pq.top();        pq.pop();        if(now.to==t) cnt++;        if(cnt==k) return now.g;        for(int i=head[now.to]; ~i; i=e[i].next)        {            Node nt;            nt.to=e[i].to;            nt.g=now.g+e[i].w;            nt.f=nt.g+dist[nt.to];            pq.push(nt);        }    }    return -1;}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        tot1=0,tot2=0;        memset(head1,-1,sizeof(head1));        memset(head2,-1,sizeof(head2));        for(int i=0; i<m; i++)        {            int x,y,w;            scanf("%d%d%d",&x,&y,&w);            addEdge1(x,y,w);            addEdge2(y,x,w);        }        scanf("%d%d%d",&S,&T,&K);        SPFA(T,head2,edge2);        printf("%d/n",A_Star(S,T,K,head1,edge1));    }    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久久久亚洲美女啪啪 | 久色精品 | av在线免费观看播放 | 97se亚洲综合在线韩国专区福利 | 免费在线观看中文字幕 | 一区国产精品 | 精品国产一区二区久久 | 精品三级内地国产在线观看 | 久久精品一区二区三区不卡牛牛 | 黄色99视频 | 色婷婷a | 逼特逼视频在线观看 | 草草视频在线播放 | av播播 | 久久里面有精品 | 欧美视频一二三区 | 成人在线视频免费播放 | 免费黄色成人 | 毛片一级网站 | 久久超碰99 | 国产成年人在线观看 | 日韩午夜片| 久久老司机精品视频 | 久久精品一区视频 | 欧美精品亚洲人成在线观看 | 久久精品一区二区三区不卡牛牛 | 日本中文字幕久久 | 欧美一级视频网站 | 黄视频网站免费在线观看 | 欧美h版在线观看 | 91一区二区三区久久久久国产乱 | 国产精品久久久久久久久久久久午夜 | 国产成人精品午夜视频' | 久久久成人免费视频 | www噜噜偷拍在线视频 | 国产精品免费久久久久 | 日韩视频www | 免费在线性爱视频 | 法国极品成人h版 | 国产免费久久久久 | 日韩午夜一区二区三区 |