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 ...
随机推荐
- alg-查找只出现一次的数
//只有2个数出现1次,其余的数都出现2次 class Solution { public: vector<int> singleNumber(const vector<int> ...
- docker-compose 基于Dockerfile 安装并启动redis容器的血案
前言 为了实现"一键部署"的目的,我采用Dockerfile 和 docker-compose来实现自己的目的.这个过程中,我怎么也无法启动自己的redis-server服务. 目 ...
- flex实现三列布局
css3新引入的flex在某些情况下布局非常实用 因为它是弹性盒子所以自适应效果会很棒 不过各项布局方案还是各有优劣 <!DOCTYPE html> <html lang=" ...
- AJ学IOS 之CoreLocation反地理编码小Demo输入经纬度得到城市
AJ分享,必须精品 一:效果 输入经纬度,可以得到相应的地名 二:思路 跟地里编码差不多 1.获取用户输入的经纬度 2.根据用户输入的经纬度创建CLLocation对象 3.根据CLLocation对 ...
- AJ学IOS 之小知识之_xcode插件的删除方法_自动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自动提示,
AJ分享,必须精品 一:解决解决自动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自动提示 其实,插件神马的我们自己也能写,并没有想象中的那么难,不过目前我们还是先解决当前问题 在做微 ...
- JAVA—HashMap
一些关于hashmap的学习笔记 1.HashMap底层实现原理 在JDK1.7中HashMap是以数组加链表的形式组成的,在JDK1.8之后新增了红黑树的组成结构,当链表大于8并且容量大于64时,链 ...
- python基础:如何使用python pandas将DataFrame转换为dict
之前在知乎上看到有网友提问,如何将DataFrame转换为dict,专门研究了一下,pandas在0.21.0版本中是提供了这个方法的.下面一起学习一下,通过调用help方法,该方法只需传入一个参数, ...
- L19深度学习中的优化问题和凸性介绍
优化与深度学习 优化与估计 尽管优化方法可以最小化深度学习中的损失函数值,但本质上优化方法达到的目标与深度学习的目标并不相同. 优化方法目标:训练集损失函数值 深度学习目标:测试集损失函数值(泛化性) ...
- Python - 实现文件名自动更改,避免同名文件被覆盖的两个解决方法
[原创]转载请注明作者Johnthegreat和本文链接. 在一些不多的数据下载和生成的时候,我们倾向于直接保存为文件,当我们修改某些参数后再一次运行时,之前运行时生成的文件就被覆盖了.为了解决这个问 ...
- mybatis源码学习:基于动态代理实现查询全过程
前文传送门: mybatis源码学习:从SqlSessionFactory到代理对象的生成 mybatis源码学习:一级缓存和二级缓存分析 下面这条语句,将会调用代理对象的方法,并执行查询过程,我们一 ...