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. VMware下Ubantu与Windows共享文件夹的方法

    刚刚接触linux的同学往往喜欢在windows系统下安装一个虚拟机,然后在虚拟机上进行操作,但是windows和虚拟机上的linux系统之间的文件互传往往不太方便,今天就总结一个小技巧在window ...

  2. 真正解决ASP.NET每一个页面首次访问超级慢的问题 (转载)

    原文:http://www.afuhao.com/article_articleId-219.shtml 摘要:ASP.NET页面首次打开很慢,但别的页面如果没有访问过,去访问也会慢.你也许认为它是在 ...

  3. UVa 1328 (KMP求字符串周期) Period

    当初学KMP的时候也做过这道题,现在看来还是刘汝佳的代码要精简一些,毕竟代码越短越好记,越不容易出错. 而且KMP的递推失配函数的代码风格和后面的Aho-Corasick自动机求失配函数的代码风格也是 ...

  4. “main cannot be resolved or is not a field”解决方案

    .layout.main总是在layout上有错误提示波浪线. 解决方法: (1) 删除"import android.R;". (2) 勾选上Eclipse中的"Pro ...

  5. codeforces 333B - Chips

    注意:横向纵向交叉时,只要两条边不是正中的边(当n&1!=1),就可以余下两个chip. 代码里数组a[][]第二维下标 0表示横向边,1表示纵向边. #include<stdio.h& ...

  6. json转csv

    import re # csv格式 # 'k1,k2,k3\nv1,v2,v3\nv4,v5,v6\n' market_list_data = { "data": [ { &quo ...

  7. test chemes

    rcmobile://messages rcmobile://badge rcmobile://dialer rcmobile://open rcmobile://sms?type=new

  8. solr4.2 solrconfig.xml配置文件简单介绍

    对于solr4.x的每个core有两个很重要的配置文件:solrconfig.xml和schema.xml,下面我们来了解solrconfig.xml配置文件. 具体很详细的内容请细读solrcofi ...

  9. 头痛的ASCII和preg_replace()

    说这个之前,大家先看下这条语句: preg_replace("/\<\?\=(\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\[\]\"\'\$\x7f- ...

  10. android直接读取数据库文件

    public class Dictionary extends Activity  implements OnClickListener, TextWatcher{     private final ...