BZOJ 2100: [Usaco2010 Dec]Apple Delivery

來源:互聯網
上載者:User

標籤:ret   head   main   cst   names   min   class   push   spfa   

題目連結:http://www.lydsy.com/JudgeOnline/problem.php?id=2100

解:

典型的最短路,從兩個點都跑一遍模板就好了,不過有點麻煩的是,如果跑dijkstra,要用堆來最佳化,如果跑spfa要用SLF(當然習慣用LLL也行)來最佳化,總之不能裸的模板去跑。

程式:

#include<iostream>#include<cstdio>#include<cstring>#include<queue>#define INF 2100000000using namespace std;struct ding{  int to,w,next;}edge[400010];struct ding2{  int p,di;  bool operator<(const ding2&a) const  {      return a.di<di;  }};int cnt,ans,n,m;int head[100010],dis[100010];priority_queue<ding2>q;void add(int u,int v,int d){edge[++cnt].to=v; edge[cnt].w=d; edge[cnt].next=head[u];head[u]=cnt;}void dijkstra(int st){  for (int i=1;i<=n;i++) dis[i]=INF;  q.push((ding2){st,0});   dis[st]=0;  while (!q.empty())  {    ding2 now=q.top(); q.pop();     if (now.di!=dis[now.p]) continue;    for (int i=head[now.p];i;i=edge[i].next)    {      int k=edge[i].to;      if (dis[k]>now.di+edge[i].w)       {        dis[k]=now.di+edge[i].w;        q.push((ding2){k,dis[k]});      }    }  }}int main(){  int st,en1,en2;  scanf("%d%d%d%d%d",&m,&n,&st,&en1,&en2);  int x,y,d;  for (int i=1;i<=m;i++)  {      scanf("%d%d%d",&x,&y,&d);      add(x,y,d); add(y,x,d);  }  dijkstra(en1);  ans+=dis[en2]+dis[st];  dijkstra(en2);  ans=min(ans,dis[en1]+dis[st]);  printf("%d\n",ans);  return 0;}

 

BZOJ 2100: [Usaco2010 Dec]Apple Delivery

聯繫我們

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