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 ...
随机推荐
- SEO误区之——静态化页面
你随便去找一个做SEO的人或者一个公司,他百分之百会让你把网页弄成纯静态页面,然后告诉你这样对搜索引擎是如何如何地好,那么我告诉你,这个做 SEO的,肯定不专业. 网页静态化这个东西,纯属以讹传讹的事 ...
- 字符集研究之多字节字符集和unicode字符集
作者:朱金灿 来源:http://blog.csdn.net/clever101 本文简介计算机中两大字符集:多字节字符集和unicode字符集的出现及关系. 首先我们须要明确的是计算机是怎样找到字符 ...
- 用循环将三个DIV变成红色
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- blind xxe攻击
最近做啊里的题的时候遇到了 http://hivesec.net/web-security/%E5%85%B3%E4%BA%8Eblind-xxe.html
- Android开发有用的三方网站
聚合数据-免费数据调用 https://www.juhe.cn/ 有赞- 免费的微商城 http://youzan.com/ 秀米微信图文编辑器 http://xiumi.us/ 禅道项目管理软件 h ...
- 【Excle数据透视】如何升序排列字段列表中的字段
数据透视表创建完毕,那么如何将字段列表中的字段修改为升序排列呢? 解决方案 更改"字段列表"为按"升序"排列 步骤 单击数据透视表任意单元格→右键单击→数据透视 ...
- struts2的BaseAction<T>继承ActionSupport实现ModelDriven<T>
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> { private sta ...
- Win8.1离线安装.Net Framework 3.5
在线安装太慢了! 只要一个命令搞掂 不希望使用Internet连接,可以使用DISM (部署映像服务和管理工具)离线部署 .NET Framework 3.5 1. Win+X选择命令提示符(管 ...
- HDU 3461 Code Lock(并查集的应用+高速幂)
* 65536kb,仅仅能开到1.76*10^7大小的数组. 而题目的N取到了10^7.我開始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 事 ...
- HDU 5056 Boring count(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5056 Problem Description You are given a string S con ...