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

题目大意:求到x距离与从x返回和的最大值

从x点到各个点最短路好求,直接用Dijkstar,但从各个点到x点却不好求,只要把路向翻转过来也变成求从x点到各个点,直接用Dijstar

dist[]记录x点到各个点的最短路径距离

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define max(a, b)(a > b ? a : b)
#define min(a, b)(a < b ? a : b)
#define INF 0xffffff
#define N 1010 int G1[N][N], G2[N][N];
bool vis[N];
int dist1[N], dist2[N], n; void Init()
{
int i, j;
memset(vis, false, sizeof(vis));
for(i = ; i <= n ; i++)
{
dist1[i] = INF;
dist2[i] = INF;
for(j = ; j <= n ; j++)
{
G1[i][j] = INF;
G2[i][j] = INF;
}
}
} void Dij(int G[][N], int dist[], int x)
{
int index, Min, i, j;
for(i = ; i <= n ; i++)
dist[i] = INF;
dist[x] = ;
for(i = ; i <= n ; i++)
{
index = ;
Min = INF;
for(j = ; j <= n ; j++)
{
if(!vis[j] && dist[j] < Min)
{
Min = dist[j];
index = j;
}
}
vis[index] = true;
for(j = ; j <= n ; j++)
{
if(!vis[j] && dist[j] > dist[index] + G[index][j])
dist[j] = dist[index] + G[index][j];
}
}
} int main()
{
int m, x, i, a, b, t, Max;
while(scanf("%d%d%d", &n, &m, &x) != EOF)
{
Init();
for(i = ; i <= m ; i++)
{
scanf("%d%d%d", &a, &b, &t);
G1[a][b] = t;
G2[b][a] = t;
}
Dij(G1, dist1, x);
memset(vis, false, sizeof(vis));
Dij(G2, dist2, x);
Max = ;
for(i = ; i <= n ; i++)
Max = max(Max, dist1[i] + dist2[i]);
printf("%d\n", Max);
}
return ;
}

POj3268 Silver Cow Party的更多相关文章

  1. POJ3268 Silver Cow Party(dijkstra+矩阵转置)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15156   Accepted: 6843 ...

  2. POJ3268 Silver Cow Party —— 最短路

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

  3. POJ3268 Silver Cow Party Dijkstra最短路

    Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...

  4. POJ-3268 Silver Cow Party---正向+反向Dijkstra

    题目链接: https://vjudge.net/problem/POJ-3268 题目大意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号X,其他牛要去拜访它并且拜访完之后要返回 ...

  5. poj3268 Silver Cow Party(两次dijkstra)

    https://vjudge.net/problem/POJ-3268 一开始floyd超时了.. 对正图定点求最短,对逆图定点求最短,得到任意点到定点的往返最短路. #include<iost ...

  6. POJ3268 Silver Cow Party【最短路】

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...

  7. poj3268 Silver Cow Party(两次SPFA || 两次Dijkstra)

    题目链接 http://poj.org/problem?id=3268 题意 有向图中有n个结点,编号1~n,输入终点编号x,求其他结点到x结点来回最短路长度的最大值. 思路 最短路问题,有1000个 ...

  8. poj3268 Silver Cow Party(农场派对)

    题目描述 原题来自:USACO 2007 Feb. Silver N(1≤N≤1000)N (1 \le N \le 1000)N(1≤N≤1000) 头牛要去参加一场在编号为 x(1≤x≤N)x(1 ...

  9. POJ3268 Silver Cow Party (建反图跑两遍Dij)

    One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...

随机推荐

  1. 百度HTTPS加密搜索有什么用?

    前段时间,我曾提到百度支持移动端HTTPS SSL加密搜索,用以保护用户隐私.最近,百度开始支持PC端HTTPS SSL加密搜索,现在可以启用 https://www.baidu.com 搜索.我很少 ...

  2. linux delete files older than 3 days

    4 down vote accepted This is easy enough (although note that this goes by a modification time more t ...

  3. LA 5846 (计数) Neon Sign

    从反面考虑,统计非单色三角形的个数. 如果从一个点出发两条不同颜色的边,那么这三个点一定构成一个非单色三角形. 枚举一个顶点,统计从这个点出发的红边的个数a[i]和蓝边的个数n - 1 - a[i], ...

  4. TIOBE 2015年5月编程语言排行榜 Visual Studio系列在上升

    TIOBE 编程语言社区排行榜是编程语言流行趋势的一个指标,每月更新,这份排行榜排名基于互联网上有经验的程序员. 课程和第三方厂商的数量.排名使用著名的搜索引擎(诸如 Google.MSN.Yahoo ...

  5. UVALive 2238 Fixed Partition Memory Management(二分完美匹配)

    题意:计算机中有一些固定大小的内存,内存越大,处理速度越快.对于一个程序,加入不同的内存空间,处理所需时间不同.现给出m个内存空间,n个程序,对于每个程序程序,有k组数据(s,t),分别表示当程序 i ...

  6. [反汇编练习] 160个CrackMe之016

    [反汇编练习] 160个CrackMe之016. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  7. 物联网操作系统HelloX V1.78测试版正式发布

    经过HelloX开发团队近四个月的努力,在HelloX V1.77版本基础上,增加许多功能特性,并对V1.77版本的一些特性进行了进一步优化之后,正式形成HelloX V1.78测试版本,经相对充分的 ...

  8. 工作中的 Vim 和 git

    ————————Vim———————— 1. gf 可以转到文件中指明路径的文件. 这样可以实现文件的快速切换. ctrl+o: A -> B,  返回A ctrl+6: A.B之间快速切换 2 ...

  9. 怎样查看Eclipse是32位还是64位

    首先进入到Eclipse的安装目录,如下图:   查找到文件名为"eclipse.ini" 文件,使用文本编辑工具,或记事本打开,如下图: 如图中的红框所示,如果是win32.x8 ...

  10. @section Right

    布局页 _BaseLayout.cshtml @RenderSection("Right", true) 视图页 Index.cshtml @{ Layout = "~/ ...