POJ 3268 Silver Cow Party 正反图最短路
题目:click here
题意:
给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出。
分析:
最短路径只需要从x到i的最短路径代表他们返回的最短路径,然后将所有边反过来,再从x到i的最短路径代表他们来参加聚会的最短路径,这样对应相加找出一个最大值就可以了,当然其实不需要将所有边反过来,只需要将map的行和列对换一下就可以了,数据比较大,所以floyd超时,用dijkstra比较好点。
邻接矩阵:~~~不知道这个题为什么邻接矩阵会比邻接表快~~~
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm> using namespace std;
typedef pair<int,int> P;
const int INF = 1e9+;
const int M = 1e3+; int n, m, x;
int f, t, c;
int cost[M][M];
int costb[M][M];
int d[M];
int db[M];
bool vis[M]; void dijkstra( int s, int *dist, int field[M][M] ) {
fill( dist, dist+n+, INF );
memset( vis, false, sizeof(vis) );
vis[s] = true;
dist[s] = ;
int tm = n;
while( tm-- ) {
int minn = INF, k = s;
for( int i=; i<=n; i++ ) {
if( !vis[i] && dist[i] < minn ) {
minn = dist[i];
k = i;
}
}
vis[k] = true;
for( int i=; i<=n; i++ ) {
if( !vis[i] && dist[i] > dist[k] + field[k][i] )
dist[i] = dist[k] + field[k][i];
}
}
} void solve() {
int ret = -INF;
dijkstra( x, d, cost ); // 正图 和 反图的最短路
dijkstra( x, db, costb );
for( int i=; i<=n; i++ )
ret = max( ret, d[i]+db[i] );
printf("%d\n", ret );
} int main() {
while( ~scanf("%d%d%d", &n, &m, &x ) ) {
for( int i=; i<=n; i++ ) {
for( int j=; j<=n; j++ ) {
cost[i][j] = INF;
costb[i][j] = INF;
}
}
for( int i=; i<m; i++ ) {
scanf("%d%d%d", &f, &t, &c );
cost[f][t] = c;
costb[t][f] = c;
}
solve();
}
return ;
}
邻接表:
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm> using namespace std;
typedef pair<int,int> P;
const int INF = 1e9+;
const int M = 1e3+; struct edge { int to, cost; };
int n, m, x;
int f, t, c;
int d[M];
int db[M];
vector<edge> G[M]; void dijkstra( int s, int *dist ) {
priority_queue< P, vector<P>, greater<P> > que;
fill( dist, dist+n+, INF );
dist[s] = ;
que.push( P(,s) );
while( !que.empty() ) {
P p = que.top(); que.pop();
int v = p.second;
if( dist[v] < p.first ) continue;
for( int i=; i<G[v].size(); i++ ) {
edge e = G[v][i];
if( dist[e.to] > dist[v] + e.cost ) {
dist[e.to] = dist[v] + e.cost;
que.push( P(dist[e.to],e.to) );
}
}
}
} void solve() {
dijkstra( x, db );
int ret = -INF;
for( int i=; i<=n; i++ ) {
dijkstra( i, d );
ret = max( ret, d[x]+db[i] );
}
printf("%d\n", ret );
}
int main() {
while( ~scanf("%d%d%d", &n, &m, &x ) ) {
for( int i=; i<m; i++ ) {
scanf("%d%d%d", &f, &t, &c );
G[f].push_back( (edge){t,c} );
}
solve();
}
return ;
}
POJ 3268 Silver Cow Party 正反图最短路的更多相关文章
- 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 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- 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 最短路
原题链接: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 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions:28457 Accepted: 12928 ...
- Poj 3268 Silver cow party 迪杰斯特拉+反向矩阵
Silver cow party 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...
随机推荐
- delphi新语法之泛型实现的对象池模板
现在的DELPHI因为支持泛型的语法,所以也能支持模板编程了. // 标准模板 unit UntPools; interface uses Classes, SysUtils, Unt ...
- 清风注解-Swift程序设计语言:Point11~15
目录索引 清风注解-Swift程序设计语言 Point 11. 数值型字面量 代码事例: let decimalInteger = // 十进制的17 let binaryInteger = 0b10 ...
- Saiku如何固定查询结果table的表头和首列
在使用saiku查询的时候,当“行”和“列”的维度内容过多时,在查看时只看到数据,不知道是什么数据,维度不清楚,得来回拖动滚动条才行,所以同事提出想要固定“表头”和“首列”. 在网上找了一些现成的插件 ...
- 2014年同年CFA考试中哪些CFA资料没有变化?
从2014年起,美国CFA协会将官方教材.题库.模拟题等CFA资料捆绑在报名费用之中,而以往可以单独选购的纸质版教材也变成了额外购买.这让非常多參加12月的CFA考生产生了借阅6月考生CFA资料的想法 ...
- ORACLE 查找字段在哪些表里存在
查找不是主键的字段在哪些表里存在: select owner, table_namefrom dba_tab_columnswhere lower(column_name)='firstname'; ...
- 二叉树的实现 -- 数据结构与算法的javascript描述 第十章
/** * 树,一种非线性的数据结构. 以分层的方式存储数据. * 一棵树最上面的节点成为根节点,如果一个节点下面有多个节点,这个节点称为父节点,下面的节点称为子节点 * 没有任何子节点的节点,陈宝国 ...
- 微信支付 v 3.3.6
文字说明: 前提:注册.申请服务号,开通微信支付. 涉及到的参数:AppId.AppSecret.原始ID(自动回复).mch_id(商户号).Key(商户密钥:自己设定.) 统一规范: 要求 认证方 ...
- 获取 web容器中的bean
public class WebContextBeanFinder { public static Object getBean(String beanId) { ServletContext ser ...
- js 模拟java 中 的map
//js模拟map Map = { obj : {}, put : function(key , value){ this.obj[key] = value; }, get : function(ke ...
- TOJ 1139.Compromise
2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是 ...