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 ...
随机推荐
- softmax回归推导
向量\(y\)(为one-hot编码,只有一个值为1,其他的值为0)真实类别标签(维度为\(m\),表示有\(m\)类别): \[y=\begin{bmatrix}y_1\\ y_2\\ ...\\y ...
- xxx 表 is marked as crashed and last (automatic?) repair 解决办法
如上图出现 xxx 表 is marked xxxx 的问题 运维那说是因为数据库非正常停掉 时 刚好有数据正在写入 数据库 导致的问题,这个没多大影响,需要 执行命令修复数据库,至于命令是什么? ...
- ASP.NET Core中配置监听URLs的五种方式
原文: 5 ways to set the URLs for an ASP.NET Core app 作者: Andrew Lock 译者: Lamond Lu 默认情况下,ASP. NET Core ...
- 深度解密 Go 语言之 sync.Pool
最近在工作中碰到了 GC 的问题:项目中大量重复地创建许多对象,造成 GC 的工作量巨大,CPU 频繁掉底.准备使用 sync.Pool 来缓存对象,减轻 GC 的消耗.为了用起来更顺畅,我特地研究了 ...
- 分屏神器PoweToys
win+~调用设置分屏界面,shift+软件拖到分屏位置
- 百度AI开发平台简介
AIstudio https://aistudio.baidu.com/aistudio/index 关于AI Studio AI Studio是基于百度深度学习平台飞桨的一站式AI开发平台,提供在线 ...
- I - Fill The Bag codeforces 1303D
题解:注意这里的数组a中的元素,全部都是2的整数幂.然后有二进制可以拼成任意数.只要一堆2的整数幂的和大于x,x也是2的整数幂,那么那一堆2的整数幂一定可以组成x. 思路:位运算,对每一位,如果该位置 ...
- JMeter在Mac下的安装
其实不论操作系统是Windows.Unix(如Mac OS).Linux(如Ubuntu)等,JMeter所需要的基础环境配置都是类似的,本文介绍JMeter for MAC的安装与环境配置. JMe ...
- 二, 连接Oracle 二
一,sqlplus操作 文件操作命令 1.start和@ 说明: 运行sql脚本 案例: sql>@ home/a.sql或是sql>start home/a.sql 2.edit 说明: ...
- 2019-2020-1 20199329《Linux内核原理与分析》第十三周作业
<Linux内核原理与分析>第十三周作业 一.本周内容概述 通过重现缓冲区溢出攻击来理解漏洞 二.本周学习内容 1.实验简介 注意:实验中命令在 xfce 终端中输入,前面有 $ 的内容为 ...