题目链接:

http://poj.org/problem?id=3268

题意:

先求出所有牛到x的最短路,再求出x到所有牛的最短路,两者相加取最大值(单向图)(可以用迪杰斯特拉,SPFA)

迪杰斯特拉:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
using namespace std; #define N 1005
#define INF 0xfffffff int n, m, x;
int a, b, t;
int G[N][N], dist1[N], dist2[N];
bool vis1[N], vis2[N]; void IN()
{
memset(vis1, false, sizeof(vis1));
memset(vis2, false, sizeof(vis2));
for(int i=; i<=n; i++)
{
dist1[i]=INF;
dist2[i]=INF;
for(int j=; j<=i; j++)
G[i][j]=G[j][i]=INF;
}
} void DIJ1()
{
dist1[x]=;
for(int i=; i<=n; i++)
{
int index=, MIN=INF;
for(int j=; j<=n; j++)
{
if(!vis1[j] && dist1[j]<MIN)
{
index=j, MIN=dist1[j];
}
}
vis1[index]=;
for(int j=; j<=n; j++)
{
dist1[j]=min(dist1[j], dist1[index]+G[index][j]);
}
}
} void DIJ2()
{
dist2[x]=;
for(int i=; i<=n; i++)
{
int index=, MIN=INF;
for(int j=; j<=n; j++)
{
if(!vis2[j] && dist2[j]<MIN)
{
index=j, MIN=dist2[j];
}
}
vis2[index]=;
for(int j=; j<=n; j++)
{
dist2[j]=min(dist2[j], dist2[index]+G[j][index]);
}
}
} int main()
{
while(scanf("%d%d%d", &n, &m, &x)!=EOF)
{
IN(); for(int i=; i<m; i++)
{
scanf("%d%d%d", &a, &b, &t);
G[a][b]=t;
} DIJ1();
DIJ2(); int ans=;
for(int i=; i<=n; i++)
ans=max(ans, dist1[i]+dist2[i]); printf("%d\n", ans);
}
return ;
}

(最短路)Silver Cow Party --POJ--3268的更多相关文章

  1. Silver Cow Party POJ - 3268 (固定起点和固定终点的最短路)

    思路:有向图.假设在X牧场参加party,从X回家的时候,以X为起点,使用一次Dijkstra算法即可.难点在于去X参加party的最短路如何求解. 这时候我们可以反向建图,即把原来有向图的方向全部反 ...

  2. kuangbin专题专题四 Silver Cow Party POJ - 3268

    题目链接:https://vjudge.net/problem/POJ-3268 题意:点X处开办排队,其他点的牛到X点去参加派对,然后从X点回到各自的点,通路是单向的,所有牛都要走最短路, 求出所有 ...

  3. ShortestPath:Silver Cow Party(POJ 3268)

    牛的聚会 题目大意:一群牛在一块农田的不同的点,现在他们都要去到同一个地方开会,然后现在从那个地方回到原来的位置,点与点之间的连线都是单向的,并且通过一个路径需要一定时间,问你现在哪只牛需要最多的时间 ...

  4. Silver Cow Party POJ - 3268

    #include<iostream> #include<queue> #include<cstring> using namespace std; +,INF=0x ...

  5. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  6. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  7. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  8. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  9. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

  10. POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路

    Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...

随机推荐

  1. axios基本使用

    1,安装axios cnpm install axios --save 2.在main.js里面引入 import axios from 'axios' /*解决在其他组件中不能用*/Vue.prot ...

  2. 单元测试使用spring注解获取bean

    在实际项目开发中经常会有单元测试,单元测试中经常会用类似这样的代码片段获取spring管理的bean @Test public void testSendEmail(){ MessageService ...

  3. SpringMVC之controller篇1

    概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...

  4. PAT 1063 计算谱半径(20)(代码)

    1063 计算谱半径(20 分) 在数学中,矩阵的"谱半径"是指其特征值的模集合的上确界.换言之,对于给定的 n 个复数空间的特征值 { a​1​​+b​1​​i,⋯,a​n​​+ ...

  5. Python.tornado.2.tornado.options

    记录Tornado-4.0.2源码的阅读,学习,分析 options.py 1. imports 部分 1.1 __future__ from __future__ import absolute_i ...

  6. BZOJ1935或洛谷2163 [SHOI2007]园丁的烦恼

    BZOJ原题链接 洛谷原题链接 很容易想到二维前缀和. 设\(S[i][j]\)表示矩阵\((0, 0)(i, j)\)内树木的棵数,则询问的矩形为\((x, y)(xx, yy)\)时,答案为\(S ...

  7. c#比delphi先进的地方

    下面是一个例子: using System;namespace HelloWorld{  class Hello{            private static string DateDiff( ...

  8. Java中创建对象的四种方法

    第一种 使用new关键字 第二种 使用反射技术:1)通过Class类的newInstance()方法:2)通过Constructor类的newInstance方法 第三种 通过Object类的clon ...

  9. Flex DateTime Format

    mx.formatter.DateFormatter var df:DateFormatter = new DateFormatter(); df.formatString = "YYYY- ...

  10. 初学者问题一oracle

    问:(待解决)如何将纵向表改成横向表?       (待解决)如何实现对大型数据范围差距不大的索引?(建什么索引树)