DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题意:有向图,所有点先走到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的更多相关文章
- 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 (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- 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 ...
- 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 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions:28457 Accepted: 12928 ...
随机推荐
- CABasicAnimation 按home键后台之后,再切回来动画就停止了
解决方法: 1. CABasicAnimation *thisAnimation = [CABasicAnimtaion animationWithKeyPath:@"transform.r ...
- 借教室(codevs 1217)
1217 借教室 2012年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descrip ...
- 使用xib需要记得的小问题
1. 图片 加载 图片上的label 不显示, 最后是因为xib 里位置动了 图片跑到最上层盖住了labe 2. 加载xib 有时候会崩 或加载不出来 先查看xib 是否有多余控件 3. 查看关联 ...
- NYOJ题目62笨小熊
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr4AAAK1CAIAAAChInrhAAAgAElEQVR4nO3dO3LjutaG4X8Szj0Qxx
- 与你相遇好幸运,Sails.js安装
官网: http://sailsjs.org Github:https://github.com/balderdashy/sails 开发文档: http://sailsjs.org/document ...
- 让那些为Webkit优化的网站也能适配IE10(转载)
转载地址:http://www.w3cplus.com/css3/adapting-your-webkit-optimized-site-for-internet-explorer-10.html 特 ...
- Newtonsoft.Json(Json.Net)学习笔记-高级使用(转)
1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称 7.动态决定属性是否序列化 8.枚举值的自定义格式化问题 9.自定义类型转换 10.全 ...
- ytu 1064: 输入三个字符串,按由小到大的顺序输出(水题,字符串处理)
1064: 输入三个字符串,按由小到大的顺序输出 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 471 Solved: 188[Submit][Sta ...
- javascript中求浏览器窗口可视区域兼容性写法
1.浏览器窗口可视区域大小 1.1 对于IE9+.Chrome.Firefox.Opera 以及 Safari:• window.innerHeight - 浏览器窗口的内部高度• window. ...
- 证明tmult_ok的正确性
csapp page124. practice problem 2.35 /* Determine whether arguments can be multiplied without overfl ...