zoukankan      html  css  js  c++  java
  • bzoj2100 [Usaco2010 Dec]Apple Delivery

    Description

    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 <= 100,000) pastures conveniently numbered from 1..P: no cowpath leads from a pasture to itself, cowpaths are bidirectional, each cowpath has an associated distance, and, best of all, it is always possible to get from any pasture to any other pasture. Each cowpath connects two differing pastures P1_i (1 <= P1_i <= P) and P2_i (1 <= P2_i <= P) with a distance between them of D_i. The sum of all the distances D_i does not exceed 2,000,000,000. What is the minimum total distance Bessie must travel to deliver both apples by starting at pasture PB (1 <= PB <= P) and visiting pastures PA1 (1 <= PA1 <= P) and PA2 (1 <= PA2 <= P) in any order. All three of these pastures are distinct, of course. Consider this map of bracketed pasture numbers and cowpaths with distances:  If Bessie starts at pasture [5] and delivers apples to pastures [1] and [4], her best path is: 5 -> 6-> 7 -> 4* -> 3 -> 2 -> 1* with a total distance of 12.

    一张P个点的无向图,C条正权路。
    CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌。(途中不必回家)
    可以先去NOI,也可以先去CMO。
    当然神犇CLJ肯定会使总路程最小,输出最小值。

    Input

    * Line 1: Line 1 contains five space-separated integers: C, P, PB, PA1, and PA2 * Lines 2..C+1: Line i+1 describes cowpath i by naming two pastures it connects and the distance between them: P1_i, P2_i, D_i

    Output

    * Line 1: The shortest distance Bessie must travel to deliver both apples

    Sample Input

    9 7 5 1 4
    5 1 7
    6 7 2
    4 7 2
    5 6 1
    5 2 4
    4 3 2
    1 2 3
    3 2 2
    2 6 3



    Sample Output

    12

    一道坑爹的最短路……意思是从s出发,要求走过t1、t2两个点的最短路

    做法不难想,分别以t1、t2为起点跑最短路,然后min(dis1[s]+dis1[t2],dis2[s]+dis2[t1])即是所求

    DIj+堆就不讲了,不加slf优化的spfa会超时

    我发现我对slf的理解好像是错的,因为我一直以为队头就是当前处理的那个点,实际上从队头提出来之后就要出队了,这时的队头应该是当前这个点的下一个点

    #include<cstdio>
    #include<cstring>
    #define N 100010
    #define M 200010
    #define mod 100001
    using namespace std;
    const int inf=0x7fffffff/11.27;
    struct edge{
    	int to,next,v;
    }e[4*M];
    int n,m,s,t1,t2,cnt,ans,t,w;
    int head[N],dis1[N],dis2[N],q[3*N];
    bool mrk[N];
    inline int min(int a,int b)
    {return a<b?a:b;}
    inline int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void ins(int u,int v,int w)
    {
    	e[++cnt].to=v;
    	e[cnt].v=w;
    	e[cnt].next=head[u];
    	head[u]=cnt;
    }
    inline void insert(int u,int v,int w)
    {
    	ins(u,v,w);
    	ins(v,u,w);
    }
    inline void spfa(int S,int *dist)  
    {  
    	for (int i=1;i<=n;i++)mrk[i]=0;
    	for (int i=1;i<=n;i++)dist[i]=inf;
    	memset(q,0,sizeof(q));
        q[0]=S;mrk[S]=1;dist[S]=0;
        t=0;w=0;
        do
        {  
            int now=q[t];
        	t=(t+1)%mod;
            for (int i=head[now];i;i=e[i].next)  
              if (dist[e[i].to]>dist[now]+e[i].v)  
              {  
                dist[e[i].to]=dist[now]+e[i].v;  
                if (!mrk[e[i].to])  
                {  
                    mrk[e[i].to]=1;  
                    if (dist[q[t]]>dist[e[i].to])
                    {
                    	t=(t-1+mod)%mod;
                    	q[t]=e[i].to;
                	}
                	else
                	{
                		w=(w+1)%mod;
                		q[w]=e[i].to;
                	}
                }  
              }  
            mrk[now]=0;  
        }
        while (t!=w);
    }  
    int main()
    {
    	m=read();n=read();s=read();t1=read();t2=read();
    	int x,y,z;
    	for (int i=1;i<=m;i++)
    	  {
    	  	x=read();y=read();z=read();
    	  	insert(x,y,z);
    	  }
    	spfa(t1,dis1);
    	spfa(t2,dis2);
    	ans=min(dis1[s]+dis1[t2],dis2[s]+dis2[t1]);
    	printf("%d",ans);
    }


    ——by zhber,转载请注明来源
  • 相关阅读:
    反转字符串
    数组
    复杂度分析(二)
    复杂度分析(一)
    业务应该这么写--特性
    5种方法快速启动一个应用程序
    业务应该这么写--表达式树
    业务应该这么写--泛型
    业务应该这么写--异常处理
    关于关系型数据库外键,要减轻数据库压力的一些说法
  • 原文地址:https://www.cnblogs.com/zhber/p/4036020.html
Copyright © 2011-2022 走看看