poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13611 | Accepted: 6138 |
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10 题目及算法分析:
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#define N 1000+20
#define INF 0x3f3f3f3f using namespace std; int map[N][N];
int dis[N], ans[N];
bool vis[N];
int n, m, s; int Dijkstra(int s)
{
int i, j, k;
for(i=1; i<=n; i++)
dis[i]=map[s][i];
vis[s]=true;
for(k=1; k<n; k++)
{
int mi=INF, pos;
for(i=1; i<=n; i++)
{
if(vis[i]==false && dis[i]<mi )
{
mi=dis[i]; pos=i;
}
}
vis[pos]=true;
for(j=1; j<=n; j++)
{
if(vis[j]==false && dis[j]>dis[pos]+map[pos][j] )
dis[j]=dis[pos]+map[pos][j];
}
}
for(i=1; i<=n; i++)
{
ans[i]=ans[i]+dis[i];
}
return 0;
} void turn_over()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<i; j++)
swap(map[i][j], map[j][i] );
}
} int main()
{
scanf("%d %d %d", &n, &m, &s);
int u, v, w; for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(i==j) map[i][j]=0;
else map[i][j]=INF;
} for(int i=0; i<m; i++)
{
scanf("%d %d %d", &u, &v, &w);
map[u][v] = w;
}
memset(vis, false, sizeof(vis));
memset(ans, 0, sizeof(ans));
Dijkstra(s);
turn_over();
memset(vis, false, sizeof(vis));
Dijkstra(s);
int cc=-1;
for(int i=1; i<=n; i++)
{
if(ans[i]>cc && ans[i]<INF )
cc=ans[i];
}
printf("%d\n", cc );
return 0;
}
poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )的更多相关文章
- POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)
题目链接: https://vjudge.net/problem/POJ-3268 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently n ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
- 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 ...
- POJ 3268 Silver Cow Party 单向最短路
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22864 Accepted: 1044 ...
随机推荐
- 2016.11.29 activiti实战--第19章--统一身份管理(含自定义用户与数组的实现)
学习资料:<Activiti实战> 第十九章 统一身份管理 本章讲解如何统一业务系统与activiti的用户管理系统. 第5章的时候已经讲解过activiti的用户与组.一般来说业务系统都 ...
- (转)来自互联网巨头的46个用户体验面试问题(谷歌,亚马逊,facebook及微软)
原文出处: uxdesign - Eleonora Zucconi 译文出处:UXRen - 邓俊杰 如果你是个正在找工作的用户体验研究员,或是一个招聘经理正急需一些启发性问题来测试你的候选人,这 ...
- [ACM] HDU 5024 Wang Xifeng's Little Plot (构造,枚举)
Wang Xifeng's Little Plot Problem Description <Dream of the Red Chamber>(also <The Story of ...
- 六种基本DCDC变换器拓扑结构
1.SEPIC电路 2.
- 浅谈iOS中MVVM的架构设计与团队协作【转载】
今天写这篇文章是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇文章的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...
- PHP中__get()和__set()的用法实例详解
php面向对象_get(),_set()的用法 一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是,对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数“__ge ...
- 玩转 eclipse:[1]如何快速找错-debug
本文摘自百度经验 原文地址如下: 玩转 eclipse:[1]如何快速找错-debu eclipse是软件开发人员必备的IDE之一. 由于语言障碍或者是经验不足,许多刚刚新手并不清楚如何高效使用ecl ...
- 【selenium+python】之Python Flask 开发环境搭建(Windows)
一.先安装python以及pip 二.其次, Python的虚拟环境安装: 在github上下载https://github.com/pypa/virtualenv/tree/master zip文 ...
- [转]浅谈Flash Socket通信安全沙箱
用过Flash socket的同学都知道,Flash socket通讯有安全沙箱问题.就是在Flash Player发起socket通信时,会向服务端获取安全策略,如果得不到服务端响应,flash将无 ...
- smarty静态缓存
缓存能让程序访问起来更加快速,调数据库的数量变少,不能实时的跟数据库同步, 一般缓存文件都放在smarty文件下cach文件夹中: 建立缓存的PHP和HTML文件: 先编辑PHP文件来查询显示数据库当 ...