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 ...
随机推荐
- 开发EXTMVC框架前需要了解的基础知识整理
1.组件选择器 目的:了解如何选择Extjs中的组件,就跟学习jquery时一定会先要学习:$()选择器一样. 常用场景: 1.在controller中的control事件中用到 ...
- 【Python】可变对象和不可变对象
Python在heap中分配的对象分成两类:可变对象和不可变对象.所谓可变对象是指,对象的内容是可变的,例如list.而不可变的对象则相反,表示其内容不可变. 不可变对象:int,string,flo ...
- 【转载】CreateProcess的用法
第一.第二个参数的用法: 例子: 使用ie打开指定的网页. 注意第二个参数是 可执行文件+命令行参数 #include "stdafx.h" #include <window ...
- .NET设计模式(13):享元模式(Flyweight Pattern)(转)
摘要:面向对象的思想很好地解决了抽象性的问题,一般也不会出现性能上的问题.但是在某些情况下,对象的数量可能会太多,从而导致了运行时的代价.那么我们如何去避免大量细粒度的对象,同时又不影响客户程序使用面 ...
- Docker学习资料
官方Docker:https://www.docker.com/ CSDN Docker : http://docker.csdn.net/ Docker中文社区:http://www.docker. ...
- HDU4776 Ants(Trie && xor)
之前mark下来的一道题,今天填一下坑. 题意是这样子的.给你一棵边上有权的树.然后有树上两点(u,v)的路径有n*(n-1)条,路径(u,v)的权值是边权的xor. 然后下面有m个询问,询问你n*( ...
- iOS视频压缩
// // ViewController.m // iOS视频测试 // // Created by apple on 15/8/19. // Copyright (c) 2015年 tqh. All ...
- linux查找有用日志常用技巧
对于高级测试人员来说.需要有快速定位问题的能力,而查看有效的日志就是其中有效的方法之一,然而服务器上的日志多如牛毛,如何快速从中找出所需信息非常重要,以下是我在工作中用到的查找日志的简单命令,希望对大 ...
- CF 353A Domino
#include<stdio.h> #include<math.h> int main() { int i,n; int x,y; int m1,m2,m3,m4; while ...
- linuxlcd驱动程序编写 mini2440(w35)
先说lcd驱动的框架吧! lcd驱动也有自己的框架,如果没有框架,要我们自己完成所有lcd驱动的代码编写那将是很痛苦的一件事. lcd驱动主要依赖于一个文件,fbmem.c 其实它还依赖几个文件 不 ...