POJ 3268_Silver Cow Party
题意:
n个地方,标号1~n,每个地方都有一头牛,现在要他们都去往标号为x的地方,再从x返回,每条道路都是单向的,求所有牛走的来回的最短路中的最大值。
分析:
注意在求每头牛走到x时,挨个算肯定超时,可以在将道路反向处理,都变成从x出。之前用vector模拟邻接表超时,后来用链表和数组分别模拟了邻接表,好像数组模拟的更快一些。
代码:
链表:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn =1005, maxm = 100005, INF = 0x3fffffff;
struct edge
{
int to, w;
edge* next;
};
int n, m, x;
int in[maxn], d[maxn], d1[maxn];
edge* head[maxn];
edge* head2[maxn];
queue<int>q;
void spfa()
{
fill(d, d+1+n,INF);
fill(in, in + 1 + n, 0);
d[x] = 0;
q.push(x);
in[x] = 1;
while(!q.empty()){
int a = q.front();
edge* t =head[a];q.pop();
in[a] = 0;
while(t!=NULL){
if(d[t->to]>d[a]+t->w){
d[t->to] = d[a] + t->w;
if(!in[t->to]){q.push(t->to);in[t->to] = 1;}
}
t = t->next;
}
}
return;
}
void spfa2()
{
fill(d1, d1+1+n,INF);
fill(in, in + 1 + n, 0);
d1[x] = 0;
q.push(x);
in[x] = 1;
while(!q.empty()){
int a = q.front();
edge* t=head2[a];q.pop();
in[a] = 0;
while(t!=NULL){
if(d1[t->to]>d1[a]+t->w){
d1[t->to] = d1[a] + t->w;
if(!in[t->to]){q.push(t->to);in[t->to] = 1;}
}
t = t->next;
}
}
return;
}
int main (void)
{
scanf("%d%d%d",&n,&m,&x);
int t1, t2, t3;
int res = 0;
memset(head, 0,sizeof(head));
for(int i = 0; i <m; i++){
scanf("%d%d%d", &t1, &t2, &t3);
edge* temp=new edge;
edge* temp2 = new edge;
temp->to = t2;
temp->w= t3;
temp->next = NULL;
if(head[t1]==NULL) head[t1] = temp;
else {
temp->next =head[t1];
head[t1] = temp;
}
temp2->to = t1;
temp2->w = t3;
temp2->next = NULL;
if(head2[t2]==NULL) head2[t2] = temp2;
else {
temp2->next = head2[t2];
head2[t2] = temp2;
}
}
spfa();
spfa2();
for(int i = 1; i <= n; i++){
if(i!=x&&res<d[i]+d1[i]) res = d[i]+d1[i];
}
printf("%d\n",res);
}
数组:
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
int n, m, x;
struct edge{
int to,w;
int next;
};
const int maxn =1005, maxm = 100005, INF = 0x3fffffff;
int head[maxn], head2[maxn];
edge e[maxm], re[maxm];
int in[maxn], d[maxn], d1[maxn];
void spfa()
{
fill(d, d+n+1,INF);
fill(in, in+n+1,0);
queue<int>q;
q.push(x);
in[x] = 1;
d[x] = 0;
while(!q.empty()){
int tmp = q.front();q.pop();
in[tmp] = 0;
int t = head[tmp];
while(t!=-1){
if(d[e[t].to] > d[tmp] + e[t].w){
d[e[t].to] = d[tmp] + e[t].w;
if(!in[e[t].to]){
q.push(e[t].to);
in[e[t].to] = 1;
}
}
t = e[t].next;
}
}
}
void spfa2()
{
fill(d1,d1+n+1,INF);
fill(in, in+n+1,0);
queue<int>q;
q.push(x);
in[x] = 1;
d1[x] = 0;
while(!q.empty()){
int tmp = q.front();q.pop();
in[tmp] = 0;
int t = head2[tmp];
while(t!=-1){
if(d1[re[t].to] > d1[tmp]+re[t].w){
d1[re[t].to] = d1[tmp] + re[t].w;
if(!in[re[t].to]){
q.push(re[t].to);
in[re[t].to] = 1;
}
}
t = re[t].next;
}
}
}
int main (void)
{
scanf("%d%d%d",&n,&m,&x);
int a, b ,c;
fill(head, head+n+1, -1);
fill(head2, head2+n+1, -1);
for(int i = 0; i < m; i++){
scanf("%d%d%d",&a,&b,&c);
e[i].to = b, e[i].w = c;
e[i].next = head[a];
head[a] = i;
re[i].to = a, re[i].w =c;
re[i].next = head2[b];
head2[b] = i;
}
spfa();
spfa2();
int res = 0;
for(int i = 1; i <= n ; i++){
if(i!=x&&res<d[i]+d1[i]) res = d[i]+d1[i];
}
printf("%d\n",res);
}
POJ 3268_Silver Cow Party的更多相关文章
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
- poj 3348 Cow 凸包面积
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8122 Accepted: 3674 Description ...
- POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)
POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包) Description N (1 ≤ N ...
- POJ 3176 Cow Bowling(dp)
POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- POJ 2375 Cow Ski Area(强连通)
POJ 2375 Cow Ski Area id=2375" target="_blank" style="">题目链接 题意:给定一个滑雪场, ...
- Poj 3613 Cow Relays (图论)
Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...
- POJ 3660 Cow Contest
题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
随机推荐
- CF949B A Leapfrog in the Array
思路: 最终的时候,对于位置p,若p是奇数,则该位置的元素是(p + 1) / 2:若p是偶数,需要从p开始不断地迭代寻找上一次跳跃所处的位置(p = p + n - p / 2),直到p是奇数为止. ...
- javascript之input获取的时间减1秒&&t时间恢复
将输入得到的时间减少1秒:20:00:00 ——— 19:59:59 方法一:普通时间转换 endDateMap(date){ var h = new Date(date).getHours ...
- jsp 页面获取当前路径
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 迅为嵌入式4412平台兼容3G/4G模块的安卓开发板
安卓开发板特点说明: 1. 配备16G固态硬盘EMMC存储 2. 64位双通道2GB内存 三星S5M8767电源管理 板载高精度GPS模块 CAN,RS-485等工业接口 板载WIFI蓝牙模块,陀螺仪 ...
- day24-1 元类
目录 元类 类的组成 内置函数 exec() class关键字创建类原理 自定义元类控制类的创建 自定义元类控制类实例化 自定义元类后对象属性查找顺序 元类 在python中一切皆对象,name我们用 ...
- 用cesium本身添加水纹效果
参考网站:https://blog.csdn.net/XLSMN/article/details/78752669 1.首先来看一下整体效果 2.具体方法如下: 首先,你必须有两张很重要的图片,你可以 ...
- 火狐加载用户配置文件 "C:\XXX\Mozilla Firefox\firefox.exe" http://192.168.1.1:8080 -profile ../kkk
"C:\XXX\Mozilla Firefox\firefox.exe" http://192.168.1.1:8080 -profile ../kkk $("#clic ...
- vue工程化之公有CSS、JS文件
1.关于公共的css 在src下面新建public.css,然后在main.js中引入进来 import '@/public.css',这样所有页面中都会使用这个样式了,如果只是部分页面需要,那还是不 ...
- RabbitMQ之项目中实战
说了那么多,还不是为了在项目中进行实战吗,在实践中检验真理,不然我学他干嘛,不能解决项目中的实际问题的技术都是耍流氓... 一.后台管理系统发送消息 瞎咧咧:后台管理系统发送消息到交换机中,然后通知其 ...
- Java中创建对象的内存图
所有人都知道面向对象思想,Java中的对象的创建在内存中是如何创建的,传智播客的视频看了一遍,把一些讲解的比较清晰的内容记录下来,方便记忆的更加深刻,Java中创建对象的过程,首先要理解JVM中栈.堆 ...