poj 3268(spfa)
http://poj.org/problem?id=3268
对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS。
感觉人生观都奔溃了,果然,题目做多了总会见到鬼的!!!!!!
心累,不想写这个题了。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream>
#define inf 0x3f
#define Max1 1010
#define Max2 100010 using namespace std;
int m,n,x;
int mark[Max1];
int next[Max1];
int u[Max2]; //把13和14行交换一下,你得到了结果会完全不同。。
int v[Max2];
int w[Max2];
int a[Max2],b[Max2],c[Max2];
int dist[Max1];
int ans[Max1];
bool vis[Max1]; queue <int >s;
void init()
{
memset( vis,false,sizeof(vis));
while( !s.empty()) s.pop();
memset(dist,inf,sizeof( dist ));
dist[x]=;
for(int i=;i<=n;i++)
mark[i]=-;
} void spfa()
{
s.push( x );
vis[ x ] = true ;
while( !s.empty() )
{
int tmp = s.front();
s.pop();
vis[ tmp ] = false;
for(int i = mark[ tmp ] ; i != - ; i = next[ i ] )
{
if( dist[ tmp ] + w[ i ] < dist[ v[ i ] ])
{
dist[ v[ i ] ] = dist[ tmp ] + w[ i ];
if( !vis[ v[ i ] ] )
{
s.push( v[ i ] );
vis[ v[ i ] ] = true;
}
}
}
}
for( int i = ; i <= n ; i++ )
ans[ i ] += dist[ i ];
} int main()
{
// freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&n,&m,&x))
{
memset( ans , , sizeof( ans ) );
init();
for( int i = ; i <= m ; i++ )
{
scanf( "%d%d%d",&a[ i ],&b[ i ],&c[ i ] );
u[ i ] = a[ i ];
v[ i ] = b[ i ];
w[ i ] = c[ i ];
next[ i ] = mark [ u[ i ] ] ;
mark[ u[ i ] ] = i;
}
spfa();
init();
for( int i = ; i <= m ; i++ )
{
v[ i ] = a[ i ];
u[ i ] = b[ i ];
w[ i ] = c[ i ];
next[ i ] = mark[ u[ i ] ];
mark[ u[ i ] ] = i;
}
spfa();
int Max = ans[];
for( int i = ; i <= n ; i++ )
if( ans[ i ] > Max ) Max = ans[ i ];
printf("%d\n",Max);
}
return ;
}
poj 3268(spfa)的更多相关文章
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- 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、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- 图论 ---- 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 SPFA+SLF优化 单源起点终点最短路
Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...
- POJ 3268 Dijkstra+priority_queue或SPFA
思路:正向建边,一遍Dijkstra,反向建边,再一遍Dijkstra.ans加在一起输出最大值. (SPFA也行--) // by SiriusRen #include <queue> ...
- Poj(2679),SPFA,邻接表(主流写法)
题目链接:http://poj.org/problem?id=3268 题意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求 ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
随机推荐
- [Unity] UGUI研究院之游戏摇杆
最近在做Unity基于UGUI的摇杆,发现了一种非常简单并且巧妙的方法,原文在这里, 不过要FQ!!http://godstamps.blogspot.tw/2015/07/unity-ugui-sc ...
- css居中总结
水平居中 1. inline和inline-*元素水平居中:text-align:center 2. block元素水平居中: block定宽:margin-left: auto; margin-ri ...
- 使用proguard混淆java web项目代码
1.首先下载proGuard.zip到本地: proguard4.5beta4.tar.zip解压开,2.新建文本文档,修改文件名为XXX.pro,然后复制下面内容到.pro -injars 'Y:\ ...
- 页面多次调用查询文章(have_posts())
通常来说一个页面只调用查询一次文章.have_posts() 如果页面,比如首页需要按照不同的查询参数调用多次文章 需要做如下处理: //loop前 $temp_query = $wp_que ...
- CentOs图形界面的开启与关闭
1.1 shell中运行 init 3 进入文本模式,同时会关闭相关的服务(Xserver 肯定关闭) 1.2 Alt+Ctrl+F1~F6到字符界面,root登陆,ps aux|grep /usr ...
- nginx同一iP多域名配置方法
文章转自:http://blog.itblood.com/nginx-same-ip-multi-domain-configuration.html 下面的是我本机的配置: server { list ...
- 2013区域赛长沙赛区现场赛 K - Pocket Cube
K - Pocket Cube Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- 安装 vue.js和第一个hello world
一.在自己的项目文件中使用npm下载vue npm install vue 二.在文件中引入vue.js 三.第一个hello world 注:scritpt代码必须写在html代码的下面
- CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)
C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 【PHP面向对象(OOP)编程入门教程】2.什么是类,什么是对象,类和对象这间的关系
类的概念:类是具有相同属性和服务的一组对象的集合.它为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和服务两个主要部分.在面向对象的编程语言中,类是一个独立的程序单位,它应该有一个类名并包括属 ...