D - Silver Cow Party J - Invitation Cards 最短路
http://poj.org/problem?id=3268
题目思路:
直接进行暴力,就是先求出举行party的地方到每一个地方的最短路,然后再求以每一个点为源点跑的最短路。
还有一种方法会快很多,就是跑两次最短路,一次正向的,另外一次反向的,因为都是只要求每一个位置到源点的最短距离,
所以这样子写就会快很多。
这个想法看完这个题目在脑袋里一闪而过没有仔细想,后来发现可以直接暴力,就忘记了,结果后面有一个数据大的就不行了。。。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 2e5 + ;
int d[maxn], dis[maxn], n, m;
struct node
{
int from, to, dist;
node(int from=,int to=,int dist=):from(from),to(to),dist(dist){}
}; struct heapnode
{
int u, d;
heapnode(int u=,int d=):u(u),d(d){}
bool operator<(const heapnode&a)const
{
return a.d < d;
}
};
vector<node>vec[maxn];
bool vis[maxn];
void dij(int s)
{
priority_queue<heapnode>que;
for (int i = ; i <= n; i++) d[i] = inf;
d[s] = ;
memset(vis, , sizeof(vis));
que.push(heapnode(s, ));
while(!que.empty())
{
heapnode x = que.top(); que.pop();
int u = x.u;
if (vis[u]) continue;
vis[u] = ;
for(int i=;i<vec[u].size();i++)
{
node e = vec[u][i];
if(d[e.to]>d[u]+e.dist)
{
d[e.to] = d[u] + e.dist;
que.push(heapnode(e.to, d[e.to]));
}
}
}
} int main()
{
int k;
scanf("%d%d%d", &n, &m, &k);
for(int i=;i<=m;i++)
{
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
vec[x].push_back(node(x, y, c));
}
int ans = ;
dij(k);
for (int i = ; i <= n; i++) dis[i] = d[i];
for(int i=;i<=n;i++)
{
if (i == k) continue;
dij(i);
// printf("dis[%d]=%d d[%d]=%d\n", i, dis[i], k, d[k]);
ans = max(ans, dis[i] + d[k]);
}
printf("%d\n", ans);
return ;
}
http://poj.org/problem?id=1511
这个和上面的题目一个意思
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e6 + ;
ll d[maxn], dis[maxn];
int n, m;
struct node
{
int from, to;
ll dist;
node(int from = , int to = , ll dist = ) :from(from), to(to), dist(dist) {}
}; struct heapnode
{
int u;
ll d;
heapnode(int u = , ll d = ) :u(u), d(d) {}
bool operator<(const heapnode&a)const
{
return a.d < d;
}
};
vector<node>vec[maxn];
bool vis[maxn];
void dij(int s)
{
priority_queue<heapnode>que;
for (int i = ; i <= n; i++) d[i] = inf;
d[s] = ;
memset(vis, , sizeof(vis));
que.push(heapnode(s, ));
while (!que.empty())
{
heapnode x = que.top(); que.pop();
int u = x.u;
if (vis[u]) continue;
vis[u] = ;
for (int i = ; i < vec[u].size(); i++)
{
node e = vec[u][i];
if (d[e.to] > d[u] + e.dist)
{
d[e.to] = d[u] + e.dist;
que.push(heapnode(e.to, d[e.to]));
}
}
}
}
int a[maxn], b[maxn];
ll c[maxn];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) vec[i].clear();
for (int i = ; i <= m; i++)
{
scanf("%d%d%lld", &a[i], &b[i], &c[i]);
vec[a[i]].push_back(node(a[i],b[i],c[i]));
}
dij();
for (int i = ; i <= n; i++)
{
dis[i] = d[i];
vec[i].clear();
}
for(int i=;i<=m;i++)
{
vec[b[i]].push_back(node(b[i], a[i], c[i]));
}
dij();
ll ans = ;
for(int i=;i<=n;i++)
{
ans += d[i] + dis[i];
}
printf("%lld\n", ans);
}
return ;
}
D - Silver Cow Party J - Invitation Cards 最短路的更多相关文章
- J - Invitation Cards 最短路
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- HDU 1535 Invitation Cards (最短路)
题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...
- POJ1511 Invitation Cards —— 最短路spfa
题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Tota ...
- POJ-1511 Invitation Cards( 最短路,spfa )
题目链接:http://poj.org/problem?id=1511 Description In the age of television, not many people attend the ...
- J - Invitation Cards
题目大意:邀请卡 在电视的时代,没有多少人会去剧院观看演出.古老的喜剧演员 Malidinesia知道这个事实.他们想传播戏剧尤其是古老的戏剧,他们在邀请卡上打印必要的信息和一些节目,一些学生被雇佣过 ...
- hdu1535 Invitation Cards 最短路
有一张图,若干人要从不同的点到同一个中间点,再返回,求总费用最小 中间点到各个点最小费用是普通的最短路 各个点到中间点最小费用其实就是将所有路径反向建边之后中间点到各个点的最小费用,同样用最短路就可以 ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- Silver Cow Party(最短路,好题)
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
随机推荐
- 5个有趣的Python小知识,结果令人意外
1 字符串驻留 如果上面例子返回True,但是下面例子为什么是False: 这与Cpython 编译优化相关,行为称为字符串驻留,但驻留的字符串中只包含字母,数字或下划线. 2 相同值的不可变对象 这 ...
- L5语言模型与数据集
本次实验使用的数据下载: jaychou_lyrics.txt 链接:https://pan.baidu.com/s/1LJSrkpV84YF61OPmjIHGIw 提取码:dj53 语言模型 一段自 ...
- Pet BFS
一天早上小明醒来时发现他的宠物仓鼠不见了. 他在房间寻找但是没找到仓鼠. 他想用奶酪诱饵去找回仓鼠. 他把奶酪诱饵放在房间并且等待了好几天. 但是可怜的小明除了老鼠和蟑螂没见到任何东西. 他找到学校的 ...
- 8. input限制手机输入
1. 只能输入数字: <input id="num" type="number" value="0" onkeyup="va ...
- 不是广告--如何学Java,我说点不太一样的学习方式
首先声明,这篇文章不是卖课程.介绍培训班的广告. 最近有不少读者通过微信问我:小白应该怎么学好 Java? 提问的人里有在校大学生.有刚参加工作的.有想转行做程序员的,还有一部分是最近找工作不顺的. ...
- php静态变量的销毁
什么都不说,先上代码: public function _childrenids($data,$cate_id,$clear=false) { static $arr = array(); if ($ ...
- Python(4)
lst = [1,2,4,8,16,32,64,128,256,512,1024,32769,65536,4294967296] # 输出 { 1:[1,2,3,8], 2:[16,32,64], 3 ...
- jstat命令查看JVM 的GC状态
转载于 https://www.cnblogs.com/alter888/p/10407952.html jstat命令可以查看堆内存各部分的使用量,以及加载类的数量.命令的格式如下: jstat ...
- C++-doctest-测试框架
C++-doctest-测试框架 C++UnitTestDoctest 测试框架 doctest 是用过的最简单好用的的单元测试框架, 只需要引用 一个头文件即可 无main 函数的测试样例 #def ...
- 怎么将swagger API导出为HTML或者PDF
文章目录 将swagger API导出为HTML或者PDF 什么是Asciidoc swagger2markup-maven-plugin asciidoctor-maven-plugin 使用命令行 ...