poj3268 最短路
//Accepted 1124 KB 0 ms #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <cmath> #include <algorithm> using namespace std; /** * This is a documentation comment block * 如果有一天你坚持不下去了,就想想你为什么走到这儿! * @authr songt */ ; ; const long long inf = 100000000000000LL; struct node { int u,v,c; node() { } node(int u,int v,int c):u(u),v(v),c(c) { } }p[imax_e],rp[imax_e]; int head[imax_n]; int next[imax_e]; int rhead[imax_n]; int rnext[imax_e]; int e; int re; long long dis[imax_n]; long long cost[imax_n]; bool vis[imax_n]; int n,m,x; void init() { e=; re=; memset(head,-,sizeof(head)); memset(next,-,sizeof(next)); memset(rhead,-,sizeof(rhead)); memset(rnext,-,sizeof(rnext)); } void addEdge(int u,int v,int c) { p[e]=node(u,v,c); next[e]=head[u]; head[u]=e++; } void raddEdge(int u,int v,int c) { rp[re]=node(u,v,c); rnext[re]=rhead[u]; rhead[u]=re++; } queue<int > q; int cnt[imax_n]; bool relax(int u,int v,int c) { if (dis[v]>dis[u]+c) { dis[v]=dis[u]+c; return true; } return false; } bool spfa(int src,int h[],int nt[],node p[]) { memset(cnt,,sizeof(cnt)); while (!q.empty()) q.pop(); memset(vis,,sizeof(vis)); q.push(src); vis[src]=true; ;i<=n;i++) dis[i]=inf; dis[src]=; while (!q.empty()) { int pre=q.front(); q.pop(); vis[pre]=false; ;i=nt[i]) { if (relax(pre,p[i].v,p[i].c) && !vis[p[i].v]) { if ((++cnt[p[i].v])>n) return false; vis[p[i].v]=true; q.push(p[i].v); } } } return true; } long long slove() { int u,v,c; init(); ;i<=m;i++) { scanf("%d%d%d",&u,&v,&c); addEdge(u,v,c); raddEdge(v,u,c); } ; spfa(x,head,next,p); ;i<=n;i++) { cost[i]=dis[i]; //printf("dis[%d]=%d\n",i,dis[i]); } spfa(x,rhead,rnext,rp); ;i<=n;i++) { //printf("cost[%d]=%d\n",i,dis[i]); if (cost[i]+dis[i]>ans) ans=cost[i]+dis[i]; } return ans; } int main() { while (scanf("%d%d%d",&n,&m,&x)!=EOF) { __int64 ans=slove(); printf("%I64d\n",ans); } ; }
poj3268 最短路的更多相关文章
- poj-3268最短路
title: poj-3268最短路 date: 2018-10-13 15:54:34 tags: acm 刷题 categories: ACM-最短路 概述 这是一道最短路的模板题,,,不过虽然是 ...
- POJ-3268(来回最短路+dijkstra算法)
Silver Cow Party POJ-3268 这题也是最短路的模板题,只不过需要进行两次求解最短路,因为涉及到来回的最短路之和. 该题的求解关键是:求解B-A的最短路时,可以看做A是起点,这就和 ...
- POJ3268 Silver Cow Party Dijkstra最短路
Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...
- POJ-3268.SilverCowParty.(最短路 + 图的转置)
本题思路:对原图和原图的逆图分别用一次最短路,找出最大值即可. 一开始是我是对每个顶点spfa搜了一波,结果判题时间巨长,还好这个题的数据量不是很大,所以就用了另一种思路. 参考代码:spfa单结点爆 ...
- (最短路)Silver Cow Party --POJ--3268
题目链接: http://poj.org/problem?id=3268 题意: 先求出所有牛到x的最短路,再求出x到所有牛的最短路,两者相加取最大值(单向图)(可以用迪杰斯特拉,SPFA) 迪杰斯特 ...
- POJ3268 Silver Cow Party【最短路】
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...
- 来回最短路POJ3268
这个题得主要考点在于给你的图是去了再回来得有向图,如何模块化解决呢就是转变图的方向,我们根据初始得放心求出每个点到x得最短路,然后转变所有路得方向再求出所有点到x得最短路,最后一相加就是最后的来回了~ ...
- POJ3268【最短路】
题意: n个点m条有向边,每个点有一头牛,每头牛会沿着各自的最短路先到x点,然后又从x点到各自的点,求这些牛中间最短路程最大的牛. 思路: 从x点到各点的最短路不用说了,裸的最短路: 但是从所有点到x ...
- POJ3268 Silver Cow Party —— 最短路
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
随机推荐
- [转] 关于ubuntu的sources.list总结
点击阅读原文 一.作用 文件/etc/apt/sources.list是一个普通可编辑的文本文件,保存了ubuntu软件更新的源服务器的地址.和sources.list功能一样的是/etc/apt/s ...
- 【转】appStore上传苹果应用程序软件发布流程
转载地址:http://blog.sina.com.cn/s/blog_68661bd801019uzd.html 首先确定帐号是否能发布, https://developer.apple.com/a ...
- Team Queue (uva540 队列模拟)
Team Queue Queues and Priority Queues are data structures which are known to most computer scientist ...
- Jmeter函数 唯一取值 笔记
java sample时: result.setIdleTime(毫秒):设置waste time ,jmeter在统计响应时间时会自动减去此函数设置的时间 result.setSuccessful( ...
- 2016年12月20日 星期二 --出埃及记 Exodus 21:15
2016年12月20日 星期二 --出埃及记 Exodus 21:15 "Anyone who attacks his father or his mother must be put to ...
- 用JavaBean实现数据库的连接和关闭,在jsp页面输出数据库中student表中学生的信息
package com.hanqi.test; import java.sql.*; public class XveSheng { Connection conn; Statement st; Re ...
- BZOJ 2600: [Ioi2011]ricehub
2600: [Ioi2011]ricehub Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 628 Solved: 325[Submit][Stat ...
- git学习笔记总结
git试免费的开源的分布式版本控制系统,github是一个用git做版本控制的项目托管平台.说白了git就是帮忙你管理你开发的代码,代码每次修改的历史,多人更好的一起开发项目. 分布式版本控制系统,每 ...
- pyc 文件反编译 py uncompyle2
一.工具 https://github.com/Mysterie/uncompyle2 -----------------------2016-6-3 11:24:19-- source:[1]用un ...
- Knockout学习笔记之二($root,$parent及$data的区别)
以下是我从Google上找到的一个例子,非常生动形象,我修改了部分代码,具体内容如下: 对于$root 与$parent的区别: $root refers to the view model appl ...