题目传送门 1 2

题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程

分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路.

POJ 3268

//#include <bits/stdc++.h>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std; typedef long long ll;
const int N = 1e6 + 5;
const int E = N;
const int INF = 0x3f3f3f3f;
struct Edge {
int u, v, w, nex;
Edge() {}
Edge(int u, int v, int w, int nex) : u (u), v (v), w (w), nex (nex) {}
bool operator < (const Edge &r) const {
return w > r.w;
}
}edge[2][E];
int head[N];
int d[2][N];
bool vis[N];
int n, m, x, e; inline int read(void) {
int f = 1, ret = 0; char ch = getchar ();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch <= '9' && ch >= '0') {
ret = ret * 10 + ch - '0';
ch = getchar ();
}
return ret * f;
} void init(void) {
memset (head, -1, sizeof (head));
e = 0;
} void add_edge(int u, int v, int w) {
edge[0][e] = Edge (u, v, w, head[u]);
head[u] = e++;
} void build_re_graph(void) {
memset (head, -1, sizeof (head));
e = 0;
for (int i=0; i<m; ++i) {
edge[1][e] = Edge (edge[0][i].v, edge[0][i].u, edge[0][i].w, head[edge[0][i].v]);
head[edge[0][i].v] = e++;
}
} void SPFA(int s, int tp) {
memset (d[tp], INF, sizeof (d[tp]));
memset (vis, false, sizeof (vis));
d[tp][s] = 0; vis[s] = true;
queue<int> que; que.push (s);
while (!que.empty ()) {
int u = que.front (); que.pop ();
vis[u] = false;
for (int i=head[u]; ~i; i=edge[tp][i].nex) {
int v = edge[tp][i].v, w = edge[tp][i].w;
if (d[tp][v] > d[tp][u] + w) {
d[tp][v] = d[tp][u] + w;
if (!vis[v]) {
vis[v] = true; que.push (v);
}
}
}
}
} void Dijkstra(int s, int tp) {
memset (vis, false, sizeof (vis));
memset (d[tp], INF, sizeof (d[tp])); d[tp][s] = 0;
priority_queue<Edge> que; que.push (Edge (0, s, d[tp][s], 0));
while (!que.empty ()) {
int u = que.top ().v; que.pop ();
if (vis[u]) continue;
for (int i=head[u]; ~i; i=edge[tp][i].nex) {
int v = edge[tp][i].v, w = edge[tp][i].w;
if (!vis[v] && d[tp][v] > d[tp][u] + w) {
d[tp][v] = d[tp][u] + w; que.push (Edge (0, v, d[tp][v], 0));
}
}
}
} int get_max(void) {
int ret = 0;
for (int i=1; i<=n; ++i) {
ret = max (ret, d[0][i] + d[1][i]);
}
return ret;
} int main(void) {
while (scanf ("%d%d%d", &n, &m, &x) == 3) {
init ();
for (int u, v, w, i=0; i<m; ++i) {
scanf ("%d%d%d", &u, &v, &w);
add_edge (u, v, w);
}
//SPFA (x, 0);
Dijkstra (x, 0);
build_re_graph ();
//SPFA (x, 1);
Dijkstra (x, 1);
int ans = get_max ();
printf ("%d\n", ans);
} return 0;
}

还有一道类似的题目,是求最大和,其实都是一样的

POJ 1511 

//#include <bits/stdc++.h>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std; typedef long long ll;
const int N = 1e6 + 5;
const int E = N;
const int INF = 0x3f3f3f3f;
struct Edge {
int u, v, w, nex;
Edge() {}
Edge(int u, int v, int w, int nex) : u (u), v (v), w (w), nex (nex) {}
bool operator < (const Edge &r) const {
return w > r.w;
}
}edge[2][E];
int head[N];
int d[N];
bool vis[N];
int n, m, e; inline int read(void) {
int f = 1, ret = 0; char ch = getchar ();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch <= '9' && ch >= '0') {
ret = ret * 10 + ch - '0';
ch = getchar ();
}
return ret * f;
} void init(void) {
memset (head, -1, sizeof (head));
e = 0;
} void add_edge(int u, int v, int w) {
edge[0][e] = Edge (u, v, w, head[u]);
head[u] = e++;
} void build_re_graph(void) {
memset (head, -1, sizeof (head));
e = 0;
for (int i=0; i<m; ++i) {
edge[1][e] = Edge (edge[0][i].v, edge[0][i].u, edge[0][i].w, head[edge[0][i].v]);
head[edge[0][i].v] = e++;
}
} void SPFA(int s, int tp) {
memset (d, INF, sizeof (d));
memset (vis, false, sizeof (vis));
d[s] = 0; vis[s] = true;
queue<int> que; que.push (s);
while (!que.empty ()) {
int u = que.front (); que.pop ();
vis[u] = false;
for (int i=head[u]; ~i; i=edge[tp][i].nex) {
int v = edge[tp][i].v, w = edge[tp][i].w;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
if (!vis[v]) {
vis[v] = true; que.push (v);
}
}
}
}
} ll get_sum(void) {
ll ret = 0;
for (int i=1; i<=n; ++i) {
ret += d[i];
}
return ret;
} int main(void) {
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &n, &m);
init ();
for (int u, v, w, i=0; i<m; ++i) {
//scanf ("%d%d%d", &u, &v, &w);
u = read (); v = read (); w = read ();
add_edge (u, v, w);
}
SPFA (1, 0);
ll ans = get_sum ();
build_re_graph ();
SPFA (1, 1);
ans += get_sum ();
printf ("%lld\n", ans);
} return 0;
}

  

DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards的更多相关文章

  1. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  2. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  3. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  4. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  6. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  7. Poj 3268 Silver cow party 迪杰斯特拉+反向矩阵

    Silver cow party 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...

  8. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  9. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:28457   Accepted: 12928 ...

随机推荐

  1. supersr--图片轮播逻辑

    // //  ViewController.m // 图片轮播 // //  Created by apple on 14-5-18. //  Copyright (c) 2014年  All rig ...

  2. 自定义Log模块

    BuildConfig.DEBUG默认为true,在为app打上SignedKeyStore之后就为false, // Log控制器 MyLoger.openDebutLog(true); MyLog ...

  3. cf118A(水题)

    题意就是讲给出的字符串元音字母去掉,在每个辅音字母前加点,且小写输出...注意y也要去掉(以我英语挂科的水平也知道y是辅音字母)... 水题.. 直接上代码好了... #include <ios ...

  4. IE6中使用通用选择器模拟子选择器效果

    IE6及更低版本不支持高级选择器:IE7有个bug,对于子选择器和相邻同胞选择器,如果父元素和子元素有HTML注释,会出问题. 下面我们使用通用选择器来模拟子选择器的效果. 原理:首先在所有后代上应用 ...

  5. Maven构建Hadoop Maven构建Hadoop工程

    一.安装maven linux eclipse3.6.1 maven安装 二:官网依赖库 我们可以直接去官网查找我们需要的依赖包的配置pom,然后加到项目中. 官网地址:http://mvnrepos ...

  6. 两个viewport的故事(第二部分)

    原文:http://www.quirksmode.org/mobile/viewports2.html 在这个迷你系列的文章里边我将会解释viewport,以及许多重要元素的宽度是如何工作的,比如&l ...

  7. ASP.NET MVC中Controller返回值类型ActionResult

    1.返回ViewResult视图结果,将视图呈现给网页 public class TestController : Controller { //必须存在Controller\Test\Index.c ...

  8. Dubbo集成Spring与Zookeeper实例

    >>Dubbo最佳实践 使用Dubbo结合Zookeeper和Spring,是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和 ...

  9. Redis笔记(七)Java实现Redis消息队列

    这里我使用Redis的发布.订阅功能实现简单的消息队列,基本的命令有publish.subscribe等. 在Jedis中,有对应的java方法,但是只能发布字符串消息.为了传输对象,需要将对象进行序 ...

  10. 让ASP.NET MVC不使用jsonp也可以跨域访问

    跨域问题仅仅发生在Javascript发起AJAX调用,或者Silverlight发起服务调用时,其根本原因是因为浏览器对于这两种请求,所给予的权限是较低的,通常只允许调用本域中的资源,除非目标服务器 ...