BZOJ 2100: [Usaco2010 Dec]Apple Delivery

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2100

解:

典型的最短路,从两个点都跑一遍模板就好了,不过有点麻烦的是,如果跑dijkstra,要用堆来优化,如果跑spfa要用SLF(当然习惯用LLL也行)来优化,总之不能裸的模板去跑。

程序:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define INF 2100000000
using 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;
}
时间: 2024-08-05 11:17:10

BZOJ 2100: [Usaco2010 Dec]Apple Delivery的相关文章

【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)

http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t 然后看了题解加了(加错了T_T)还是tle..我就怀疑数据了... 噗 原来我有个地方打错了.. 这个spfa的队列优化真神.. #include <cstdio> #include <cstring> using namespace std; #define rep(i, n) fo

BZOJ2100 [Usaco2010 Dec]Apple Delivery

水水更健康...话说回来,这真的是水题?T T 首先,容易想到: 令ans1 = t1为源,到s和t2的距离之和:ans2 = t2为源,到s和t1的距离之和 ans = min(ans1, ans2) 然后,开始写单元最短路...spfa... 1 /************************************************************** 2 Problem: 2100 3 User: rausen 4 Language: C++ 5 Result: Tim

BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )

dp( l , r ) = sum( l , r ) - min( dp( l + 1 , r ) , dp( l , r - 1 ) ) 被卡空间....我们可以发现 l > r 是无意义的 , 所以可以省下一半的空间 -------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<a

bzoj 2097: [Usaco2010 Dec]Exercise 奶牛健美操【二分+树形dp】

二分答案,然后dp判断是否合法 具体方法是设f[u]为u点到其子树中的最长链,每次把所有儿子的f值取出来排序,如果某两条能组合出大于mid的链就断掉f较大的一条 a是全局数组!!所以要先dfs完子树才能填a!! #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=1000005; int n,m,h

[Usaco2010 Dec]Exercise 奶牛健美操

[Usaco2010 Dec]Exercise 奶牛健美操 题目 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径.简单的说来, 这些点的布局就是一棵树,且每条边等长,都为1. 对于给定的一个奶牛路径集合,精明的奶牛们会计算出任意点对路径的最大值, 我们称之为这个路径集合的直径.如果直径太大,奶牛们就会拒绝锻炼. Farmer John把每个点标记为1..V

bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱*

bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱 题意: 给个序列,A与B轮流取数,谁取的数总和大谁赢.每次只能取序列两端,问A能取的数总和最大是多少.假设两人都用最优策略.序列大小≤5000 题解: dp.f[i][j][0]=max(f[i+1][j][1]+a[i],f[i][j-1][1]+a[j]),f[i][j][1]=min(f[i+1][j][0],f[i][j-1][0]). 代码: 1 #include <cstdio> 2 #includ

2102: [Usaco2010 Dec]The Trough Game

2102: [Usaco2010 Dec]The Trough Game Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 117  Solved: 84[Submit][Status] Description Farmer John and Bessie are playing games again. This one has to do with troughs of water. Farmer John has hidden N (1 <= N

洛谷P3003 [USACO10DEC]苹果交货Apple Delivery

P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000) cowpaths which are arranged as the usual graph which connects P (1 <= P <=

BZOJ2097[Usaco2010 Dec] 奶牛健美操

我猜我这样继续做水题会狗带 和模拟赛的题很像,贪心搞一下. 1 #include<bits/stdc++.h> 2 using namespace std; 3 int read(){ 4 int x=0,f=1;char ch=getchar(); 5 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 6 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getch