题意:由n个牧场,编号1到n。每个牧场有一头牛。现在在牧场x举办party,每头牛都去参加,然后再回到自己的牧场。牧场之间会有一些单向的路。每头牛都会让自己往返的路程最短。问所有牛当中最长的往返路程是多少。

思路:n最多到1000,floyd肯定超时。可以这样做,把图中所有的边先存起来,然后第一次用dijkstra求出以x为源点到每个点的最短距离。该最短距离为每头牛回家时的最短距离。然后建个新的图,将之前存的边反向加入图中。如之前有条从5到8距离为2的路,则此时向图中添加的边为从8到5距离为2的边。这样再次以x为源点求到每个点的最短距离,将两者相加,就是每头牛往返的最短距离了。求其中的最大值即可。

代码中的dijkstra算法用优先队列进行了优化。

 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#define maxn 1010
#define maxp 100010
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
int v, w, next;
node(){}
node(int a,int b){v = a; w = b;}
bool operator < (const node &cmp) const
{
if (w == cmp.w) return v < cmp.v;
else return w > cmp.w;
}
}edge[maxp];
int num_edge, head[maxn];
void init_edge()
{
num_edge = ;
memset(head, -, sizeof(head));
}
void addedge(int a,int b,int w)
{
edge[num_edge].v = b;
edge[num_edge].w = w;
edge[num_edge].next = head[a];
head[a] = num_edge++;
}
int n, m, x, dis1[maxn], dis2[maxn];
void dijkstra(int s, int dis[])
{
for (int i = ; i <= n; i++)
dis[i] = (i == s ? : inf);
priority_queue<node> q;
q.push(node(s, dis[s]));
while (!q.empty())
{
node u = q.top(); q.pop();
for (int i = head[u.v]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if (dis[v] > u.w + edge[i].w)
{
dis[v] = u.w + edge[i].w;
q.push(node(v, dis[v]));
}
}
}
}
int e[maxp][];
int main()
{
//freopen("data.in", "r", stdin);
scanf("%d%d%d",&n, &m, &x);
{
init_edge();
for (int i = ; i < m; i++)
{
scanf("%d%d%d",&e[i][],&e[i][],&e[i][]);
addedge(e[i][], e[i][], e[i][]);
}
dijkstra(x, dis1);
init_edge();
for (int i = ; i < m; i++)
addedge(e[i][], e[i][], e[i][]);
dijkstra(x, dis2);
int tmax = -inf;
for (int i = ; i <= n; i++)
tmax = max(tmax, dis1[i] + dis2[i]);
printf("%d", tmax);
}
return ;
}

POJ 3268 Silver Cow Party (Dijkstra + 优先队列)的更多相关文章

  1. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

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

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

  3. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  4. POJ 3268 Silver Cow Party (双向dijkstra)

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

  5. POJ 3268 Silver Cow Party 最短路

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

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

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

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

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

  8. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  9. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:28457   Accepted: 12928 ...

随机推荐

  1. mysql进阶二

    数据库存储数据的特点: 1.数据存放到表中,然后表再放到库中 2.一个库中可以有多张表,每张表具有唯一的表名来标识自己 3.表中有一个或多个列,列又称为“字段” 数据库常见的管理系统 mysql.or ...

  2. 第一次接触php

    一.什么是PHP PHP的中文意思:超文本预处理器,英文名字: HyperText Preprocessor. PHP通常有两层含义: (1)PHP是一个编程语言. (2)PHP是处理PHP编程语言的 ...

  3. STL学习笔记3--deque

    看这一节,是为了下一节的使用,在ogre3d里有些操作要使用到deque. C++ Deque(双向队列) 的使用 Deque结合了vector  和list  优缺点,是一种使用简单的容器. deq ...

  4. Map-Reduce基础

    1.设置文件读入分隔符 默认按行读入; 按句子读入 : conf1.set("textinputformat.record.delimiter", "."); ...

  5. java课堂 笔记

  6. vmware安装centos7 安装redis windows7访问redis

    1.在windows7中安装vmware 2.在vmware中安装centos7 3.禁用centos7自带的firewalld.service 4.安装iptables防火墙 5.安装Redis 3 ...

  7. MOS管使PIC单片机不能正常运行

    程序: #include "led.h" void InitLed(void) { TRISB &= ~0x70; ANSELB &= ~0x70; } void ...

  8. [ZJOI2012][bzoj 2816] 网络 network [LCT]

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2816 思路: 第一个条件看完暂时还没什么想法 看完第二个,发现每一个颜色都是一个森林 进而想 ...

  9. Codeforces Round #323 (Div. 2) B 贪心,暴力

    B. Robot's Task time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  10. BestCoder 2nd Anniversary/HDU 5719 姿势

    Arrange Accepts: 221 Submissions: 1401 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/2 ...