题目链接:

  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次单源最短路,最后累加求和取最小。

  

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 1004
#define M 100004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
struct xxx
{
int next,to,dis;
}a[M];
int c[N][N];
int last[N],d[N],q[N];
bool u[N];
void add(int x,int y,int z)
{
a[++lll].next=last[x];
a[lll].to=y;
a[lll].dis=z;
last[x]=lll;
}
void spfa(int s)
{
mem(d,);mem(u,);
int i,l=,r=,now,to;
q[]=s;d[s]=;
while(l!=r)
{
now=q[l=(l+)%N];
u[now]=;
for(i=last[now];i;i=a[i].next)
{
to=a[i].to;
if(d[to]>d[now]+a[i].dis)
{
d[to]=d[now]+a[i].dis;
if(!u[to])
{
u[to]=;
q[r=(r+)%N]=to;
}
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z,s,x1,y1,x2,y2;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
while(~scanf("%d",&n))
{
lll=;
mem(last,);mem(c,);
scanf("%d",&m);
scanf("%d%d%d%d%d",&s,&x1,&y1,&x2,&y2);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
spfa(s);
c[s][x1]=d[x1];c[s][x2]=d[x2];c[s][y1]=d[y1];c[s][y2]=d[y2];
spfa(x1);
c[x1][x2]=d[x2];c[x1][y1]=d[y1];c[x1][y2]=d[y2];
spfa(x2);
c[x2][x1]=d[x1];c[x2][y2]=d[y2];c[x2][y1]=d[y1];
spfa(y1);
c[y1][x2]=d[x2];c[y1][y2]=d[y2];
spfa(y2);
c[y2][y1]=d[y1];c[y2][x1]=d[x1];
ans=c[s][x1]+c[x1][x2]+c[x2][y1]+c[y1][y2];
ans=min(ans,c[s][x1]+c[x1][y1]+c[y1][x2]+c[x2][y2]);
ans=min(ans,c[s][x1]+c[x1][x2]+c[x2][y2]+c[y2][y1]);
ans=min(ans,c[s][x2]+c[x2][x1]+c[x1][y1]+c[y1][y2]);
ans=min(ans,c[s][x2]+c[x2][y2]+c[y2][x1]+c[x1][y1]);
ans=min(ans,c[s][x2]+c[x2][x1]+c[x1][y2]+c[y2][y1]);
printf("%d\n",ans);
}
return ;
}
/*
// //
*/

【最短路】FOJ 2243 Daxia like uber的更多相关文章

  1. FZU 2243 Daxia like uber

    枚举,最短路. 求出5个点出发的最短路,然后枚举一下这些点之间走的顺序. #pragma comment(linker, "/STACK:1024000000,1024000000" ...

  2. 【模拟】FOJ 2244 Daxia want to buy house

    题目链接: http://acm.fzu.edu.cn/problem.php?pid=2244 题目大意: 每月还款额=贷款本金×[月利率×(1+月利率)^还款月数]÷[(1+月利率)^还款月数-1 ...

  3. 【数论】FOJ 2238 Daxia & Wzc's problem

    题目链接: http://acm.fzu.edu.cn/problem.php?pid=2238 题目大意: 已知等差数列A(0)的首项a和公差d,求出数列A(0)前n项和,得到新数列A(1);以此类 ...

  4. FOJ有奖月赛-2016年8月(daxia专场之过四题方有奖)

    http://acm.fzu.edu.cn/contest/list.php?cid=152 主要是a题, lucas定理, 就这一版能过..  记录一下代码, 另外两个最短路  一个模拟,没什么记录 ...

  5. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  6. foj 2173 floyd+矩阵快速幂

     Problem 2173 Nostop Accept: 52    Submit: 210 Time Limit: 3000 mSec    Memory Limit : 32768 KB  Pro ...

  7. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  8. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  9. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

随机推荐

  1. Eclipse下使用Fat Jar插件对源代码进行打包

    这两天需要对一个项目进行打包,并在服务器上部署成后台服务模式进行执行,原来使用eclipse进行打包很难用,配置文件容易出错,生成的jar不能正常运行.后来发现Fat Jar Eclipse Plug ...

  2. Spire.Barcode好用的条码生在组件

    由于项目的需要,今天在网上找了一下条码的组件,发现了一个简单易用的组件,使用简单,几句代码就搞定了.

  3. 万能的everything彻底解决mysql问题

    万能的everthing D:\RECYCLER\S-1-5-21-1214440339-1123561945-1801674531-500\Dd16.0\my.ini可能之前的电脑的隐藏文件没有删除 ...

  4. IE8下ckeditor无法正常使用,提示"例外被抛出且未被接住"的解决办法

    <script language="javascript" src="ckeditor/ckeditor.js"></script> & ...

  5. 【转】iOS开发网络篇—发送json数据给服务器以及多值参数

    原文: http://www.cnblogs.com/wendingding/p/3950132.html 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 ...

  6. xml有哪些解析技术?区别是什么?

    xml有哪些解析技术?区别是什么? Answer: 有DOM,SAX,STAX等 (1):DOM:处理大型文件时其性能下降的非常厉害.这个问题是由DOM的树结构所造成的,这种结构占用的内存较多,而且D ...

  7. Spring 实例化bean的方式

    实例化bean的方式有三种: 1.用构造器来实例化 2.使用静态工厂方法实例化 3.使用实例工厂方法实例化 当采用构造器来创建bean实例时,Spring对class并没有特殊的要求, 我们通常使用的 ...

  8. 关于Asp.Net中避免用户连续多次点击按钮,重复提交表单的处理

    Web页面中经常碰到这类问题,就是客户端多次点击一个按钮或者链接,导致程序出现不可预知的麻烦. 客户就是上帝,他们也不是有意要给你的系统造成破坏,这么做的原因很大一部分是因为网络慢,点击一个操作之后, ...

  9. Valid Phone Numbers

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  10. [转]NodeJS、NPM安装配置步骤(windows版本)

    1.windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的“INSTALL” ...