【最短路】FOJ 2243 Daxia like uber

题目链接:

  http://acm.fzu.edu.cn/problem.php?pid=2243

题目大意:

  给一张N个点M条边的有向图,从s出发,把在x1的人送到y1,在x2的人送到y2用的最短距离。

题目思路:

  【最短路】

  首先就两个乘客需要送,手写可以得到6种先后次序。 

  s>x1>x2>y1>y2;

  s>x1>y1>x2>y2;

  s>x1>x2>y2>y1;

  第一个和第二个对调可得剩下三种情况。

  所以可以对s,x1,x2,y1,y2求5次单源最短路,最后累加求和取最小。

  

  1 //
  2 //by coolxxx
  3 //#include<bits/stdc++.h>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<string>
  7 #include<iomanip>
  8 #include<map>
  9 #include<memory.h>
 10 #include<time.h>
 11 #include<stdio.h>
 12 #include<stdlib.h>
 13 #include<string.h>
 14 //#include<stdbool.h>
 15 #include<math.h>
 16 #define min(a,b) ((a)<(b)?(a):(b))
 17 #define max(a,b) ((a)>(b)?(a):(b))
 18 #define abs(a) ((a)>0?(a):(-(a)))
 19 #define lowbit(a) (a&(-a))
 20 #define sqr(a) ((a)*(a))
 21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
 22 #define mem(a,b) memset(a,b,sizeof(a))
 23 #define eps (1e-8)
 24 #define J 10
 25 #define mod 1000000007
 26 #define MAX 0x7f7f7f7f
 27 #define PI 3.14159265358979323
 28 #define N 1004
 29 #define M 100004
 30 using namespace std;
 31 typedef long long LL;
 32 int cas,cass;
 33 int n,m,lll,ans;
 34 struct xxx
 35 {
 36     int next,to,dis;
 37 }a[M];
 38 int c[N][N];
 39 int last[N],d[N],q[N];
 40 bool u[N];
 41 void add(int x,int y,int z)
 42 {
 43     a[++lll].next=last[x];
 44     a[lll].to=y;
 45     a[lll].dis=z;
 46     last[x]=lll;
 47 }
 48 void spfa(int s)
 49 {
 50     mem(d,12);mem(u,0);
 51     int i,l=0,r=1,now,to;
 52     q[1]=s;d[s]=0;
 53     while(l!=r)
 54     {
 55         now=q[l=(l+1)%N];
 56         u[now]=0;
 57         for(i=last[now];i;i=a[i].next)
 58         {
 59             to=a[i].to;
 60             if(d[to]>d[now]+a[i].dis)
 61             {
 62                 d[to]=d[now]+a[i].dis;
 63                 if(!u[to])
 64                 {
 65                     u[to]=1;
 66                     q[r=(r+1)%N]=to;
 67                 }
 68             }
 69         }
 70     }
 71 }
 72 int main()
 73 {
 74     #ifndef ONLINE_JUDGE
 75     freopen("1.txt","r",stdin);
 76 //    freopen("2.txt","w",stdout);
 77     #endif
 78     int i,j,k;
 79     int x,y,z,s,x1,y1,x2,y2;
 80 //    for(scanf("%d",&cas);cas;cas--)
 81 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
 82 //    while(~scanf("%s",s))
 83     while(~scanf("%d",&n))
 84     {
 85         lll=0;
 86         mem(last,0);mem(c,0);
 87         scanf("%d",&m);
 88         scanf("%d%d%d%d%d",&s,&x1,&y1,&x2,&y2);
 89         for(i=1;i<=m;i++)
 90         {
 91             scanf("%d%d%d",&x,&y,&z);
 92             add(x,y,z);
 93         }
 94         spfa(s);
 95         c[s][x1]=d[x1];c[s][x2]=d[x2];c[s][y1]=d[y1];c[s][y2]=d[y2];
 96         spfa(x1);
 97         c[x1][x2]=d[x2];c[x1][y1]=d[y1];c[x1][y2]=d[y2];
 98         spfa(x2);
 99         c[x2][x1]=d[x1];c[x2][y2]=d[y2];c[x2][y1]=d[y1];
100         spfa(y1);
101         c[y1][x2]=d[x2];c[y1][y2]=d[y2];
102         spfa(y2);
103         c[y2][y1]=d[y1];c[y2][x1]=d[x1];
104         ans=c[s][x1]+c[x1][x2]+c[x2][y1]+c[y1][y2];
105         ans=min(ans,c[s][x1]+c[x1][y1]+c[y1][x2]+c[x2][y2]);
106         ans=min(ans,c[s][x1]+c[x1][x2]+c[x2][y2]+c[y2][y1]);
107         ans=min(ans,c[s][x2]+c[x2][x1]+c[x1][y1]+c[y1][y2]);
108         ans=min(ans,c[s][x2]+c[x2][y2]+c[y2][x1]+c[x1][y1]);
109         ans=min(ans,c[s][x2]+c[x2][x1]+c[x1][y2]+c[y2][y1]);
110         printf("%d
",ans);
111     }
112     return 0;
113 }
114 /*
115 //
116 
117 //
118 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/5784847.html