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,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...
随机推荐
- Silverlight Socket 实现收发信息
原文 http://www.cnblogs.com/ZetaChow/archive/2009/05/16/2237347.html 刚接触Silverlight的时候,除了其异步应用WCF.流媒体. ...
- DbConnectionFactory 数据库连接
/** * */package com.sprucetec.dbatch.tmsfee;import java.io.Serializable;import java.sql.Connection;i ...
- UVA1351-----String Compression-----区间DP(记忆化搜索实现)
本文出自:http://blog.csdn.net/dr5459 题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&a ...
- Address already in use: JVM_Bind <null>:8080
解决方法: 1重开eclipse,端口号被占用,或者杀掉进程
- ubuntu 安装mysql, 以及全然又一次安装的方法
sudo apt-get install mysql-server 装完后 是无法远程訪问的. 要先改动 sudo vim /etc/mysql/my.cnf 找到 bind-address = 12 ...
- Linux 内核优化
声明:本文档来自互联网整理部份加自已实验部份所得: TCP 相关部份 经常使用名词说明: retries(再试). TCP server <---> client通信状态 ...
- Request.ServerVariables完整参考
Request.ServerVariables("Url") 返回服务器地址 Request.ServerVariables("Path_Info") 客户端提 ...
- Git--廖雪峰的博客的学习笔记
为了督促自己能看完这个网站的学习教程,边看边做了些简要的笔记,记录了常用命令,其实也就是自己打了些简单的命令,好多直接就粘贴过来了,也算是一个学习的证明吧,想按详细的教程,还是要去博主的园子学习啊地址 ...
- U盘安装CentOS6.x报错:Missing ISO 9660 Image
以前都是DVD安装CentOS,这次因为装固态硬盘,然后把光驱给卸载了.所以就尝试用U盘引导安装CentOS,结果安装时竟然出现了Missing ISO 9660 Image的错误. 解决方案: 将C ...
- iOS实践01
去年放假之前大概完成了新浪微博项目,到现在也忘得差不多了,打算在重新写一遍.之前的一些笔记在新浪的博客SleenXiu,在这主要是把新浪微博以随笔的形式写在这,方便以后的复习. 先看看之前主要完成的几 ...