bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】

來源:互聯網
上載者:User

標籤:turn   live   最佳化   iostream   ios   apple   include   void   min   

洛穀資料好強啊,普通spfa開o2都過不了,要加雙端隊列最佳化
因為是雙向邊,所以dis(u,v)=dis(v,u),所以分別以pa1和pa2為起點spfa一遍,表示pb-->pa1-->pa2和pb-->pa2-->pa1,取個min即可

#include<iostream>#include<cstdio>#include<queue>using namespace std;const int N=200005,inf=2e9+7;int m,n,a,b1,b2,h[N],cnt,dis[N];bool v[N];struct qwe{    int ne,to,va;}e[N<<2];int read(){    int r=0,f=1;    char p=getchar();    while(p>‘9‘||p<‘0‘)    {        if(p==‘-‘)            f=-1;        p=getchar();    }    while(p>=‘0‘&&p<=‘9‘)    {        r=r*10+p-48;        p=getchar();    }    return r*f;}void add(int u,int v,int w){    cnt++;    e[cnt].ne=h[u];    e[cnt].to=v;    e[cnt].va=w;    h[u]=cnt;}void spfa(int s){    deque<int>q;    for(int i=1;i<=n;i++)        dis[i]=inf;    dis[s]=0,v[s]=1,q.push_back(s);    while(!q.empty())    {        int u=q.front();        q.pop_front();        v[u]=0;        for(int i=h[u];i;i=e[i].ne)            if(dis[e[i].to]>dis[u]+e[i].va)            {                dis[e[i].to]=dis[u]+e[i].va;                if(!v[e[i].to])                {                    v[e[i].to]=1;                    if(!q.empty()&&dis[q.front()]>dis[e[i].to])                        q.push_front(e[i].to);                    else                        q.push_back(e[i].to);                   }            }    }}int main(){    m=read(),n=read(),a=read(),b1=read(),b2=read();    for(int i=1;i<=m;i++)    {        int x=read(),y=read(),z=read();        add(x,y,z),add(y,x,z);    }    spfa(b1);    int ans=dis[a]+dis[b2];    spfa(b2);    printf("%d\n",min(ans,dis[a]+dis[b1]));    return 0;}

bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.