poj - 3268 Silver Cow Party (求给定两点之间的最短路)
http://poj.org/problem?id=3268
每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的农场,但是他们都想选一条最近的路,并且由于路是单向的,去的路和来的路选择可能不一样,问来去时间之和最大是多少?
这题等于给定了起点和终点,那么求出(d[x][i]+d[i][x])最大的那个即可。
开始错了几次,太不小心了,就是最后求最大值的时候,用了一个临时变量没置0,所以可能会导致错误。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxn= ;
const int INF = <<;
struct edge{
int to,cost;
edge(){}
edge(int x,int y) {
to=x;
cost=y;
}
};
typedef pair<int,int>P; int N,M,X;
vector<edge>G[maxn];
int d[maxn]; int dijkstra(int s,int t) {
priority_queue<P,vector<P>,greater<P> >que;
for(int i=;i<=N;i++) d[i]=INF;
d[s]=;
que.push(P(,s)); while(!que.empty()) {
P p=que.top(); que.pop();
int v=p.second;
if(v==t) return d[t];
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++) {
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost) {
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
} int main()
{
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int a,b,c;
while(~scanf("%d%d%d",&N,&M,&X)) {
for(int i=;i<M;i++) {
scanf("%d%d%d",&a,&b,&c);
G[a].push_back(edge(b,c));
}
int sum=;
for(int i=;i<=N;i++) {
sum=max(sum,dijkstra(i,X)+dijkstra(X,i));
}
printf("%d\n",sum);
}
return ;
}
还有一种方法,首先求出点X到各个点i的最短路径,这就是奶牛返回的最短距离,然后把路径反向,在求点X到各个点i的最短距离,就是每个点到点X的最短距离。
最后把两个距离相交即可。
不过对于反向,不是很明白。
时间直接从600多ms降到60多ms。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxn= ;
const int INF = <<;
struct edge{
int to,cost;
edge(){}
edge(int x,int y) {
to=x;
cost=y;
}
};
typedef pair<int,int>P; int N,M,X;
vector<edge>G[maxn],RG[maxn];
int d[maxn],rd[maxn]; void dijkstra(int s) {
priority_queue<P,vector<P>,greater<P> >que;
for(int i=;i<=N;i++) d[i]=INF;
d[s]=;
que.push(P(,s)); while(!que.empty()) {
P p=que.top(); que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++) {
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost) {
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
} int main()
{
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int a,b,c;
scanf("%d%d%d",&N,&M,&X);
for(int i=;i<M;i++) {
scanf("%d%d%d",&a,&b,&c);
G[a].push_back(edge(b,c));
RG[b].push_back(edge(a,c)); //存储 反向边
}
dijkstra(X); //第一次 dijkstra 是求X到各个点的距离
//for(int i=1;i<=N;i++) printf("%d\n",d[i]);
for(int i=;i<=N;i++) G[i]=RG[i]; //然后 把反向存储的边赋值给 G
// for(int i=1;i<=N;i++) {
// for(int j=0;j<G[i].size();j++)
// cout<<G[i][j].to<<" "<<G[i][j].cost<<endl;
//}
memcpy(rd,d,sizeof(d)); //把第一次的 最短距离 保存 dijkstra(X); //反向的 dijkstra 求出每个点到X的最短距离
//for(int i=1;i<=N;i++) printf("%d\n",d[i]);
int sum=; //枚举最大和
for(int i=;i<=N;i++) {
sum=max(sum,d[i]+rd[i]);
}
printf("%d\n",sum);
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 ...
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12674 Accepted: 5651 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- poj 3268 Silver Cow Party(最短路)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17017 Accepted: 7767 ...
随机推荐
- 02.Redis主从集群的Sentinel配置
1.集群环境 1.Linux服务器列表 使用4台CentOS Linux服务器搭建环境,其IP地址如下: 192.168.110.100 192.168.110.101 192.168.110.102 ...
- poj 1463 Strategic game
题目链接:http://poj.org/problem?id=1463 题意:给出一个无向图,每个节点只有一个父亲节点,可以有多个孩子节点,在一个节点上如果有一位战士守着,那么他可以守住和此节点相连的 ...
- 【LCA】CodeForce #326 Div.2 E:Duff in the Army
C. Duff in the Army Recently Duff has been a soldier in the army. Malek is her commander. Their coun ...
- Java获取项目中的路径 分类: Java Game 2014-08-14 10:17 122人阅读 评论(0) 收藏
在项目中经常需要获取某个文件的路径: 在这里提供一些获取路径的方法.. 1.此种方式获取的路径,是当前类所在的路径: UserDAOTest.class.getResource("UserD ...
- vs2008 添加与修改模板.
添加 我的模板: 路径: C:\Users\Administrator\Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C# ...
- .NET设计模式(17):命令模式(Command Pattern)(转)
概述 在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”.但在某些场合,比如要对行为进行“记录.撤销/重做.事务”等处理,这种无法抵御变化的紧耦合是不合适的.在这种情况下,如何将“行为 ...
- ASP.NET 大文件上传的简单处理
在 ASP.NET 开发的过程中,文件上传往往使用自带的 FileUpload 控件,可是用过的人都知道,这个控件的局限性十分大,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的 ...
- HDOJ 1466 计算直线的交点数
将n 条直线排成一个序列,直线2和直线1最多只有一个交点,直线3和直线1,2最多有两个交点,......,直线n 和其他n-1条直线最多有n-1个交点.由此得出n条直线互不平行且无三线共点的最多交点数 ...
- MVC 中 Razor 无限分类的展示
在MVC的Razor视图展示无级分类的办法,在网上看了很多资料,大多搞得很高大上.可能本人水平有限,实在是不会用. 那我就用最简单爆力的办法来做. Model: public class NewsCa ...
- POJ 1552
#include<iostream> using namespace std; int main() { ]; int i,j; ; do{ sum=; ;num[i-]!=&&a ...