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 ...
随机推荐
- 【课程学习】课程2:十行代码高效完成深度学习POC
本文用户记录黄埔学院学习的心得,并补充一些内容. 课程2:十行代码高效完成深度学习POC,主讲人为百度深度学习技术平台部:陈泽裕老师. 因为我是CV方向的,所以内容会往CV方向调整一下,有所筛检. 课 ...
- day18作业
作业: # 1.编写课上讲解的有参装饰器准备明天默写 def auth(file_type): def outer(func): def inter(*args,**kwargs): if file_ ...
- 转:Cookies 和 Session的区别
转自:http://blog.csdn.net/axin66ok/article/details/6175522 1.cookie 是一种发送到客户浏览器的文本串句柄,并保存在客户机硬盘上,可以用来在 ...
- MySQL使用mysqldump+binlog完整恢复被删除的数据库
(一)概述 在日常的MySQL数据库运维过程中,可能会遇到用户误删除数据,常见的误删除数据操作有: 用户执行delete,因为条件不对,删除了不应该删除的数据(DML操作): 用户执行update,因 ...
- .NET Core 初识
什么是 ASP.NET Core? ASP.NET Core 是一个新的开源和跨平台的框架,用于构建如 Web 应用.物联网(IoT)应用和移动后端应用等连接到互联网的基于云的现代应用程序.ASP.N ...
- kubernetes的headless service介绍
headless service是一个特殊的ClusterIP类service,这种service创建时不指定clusterIP(--cluster-ip=None),因为这点,kube-proxy不 ...
- MySQL主从复制,主主复制,半同步复制
实验环境: 系统:CentOS Linux release 7.4.1708 (Core) mariadb:mariadb-server-5.5.56-2.el7.x86_64 node1:172.1 ...
- blink测试技术介绍
引言: flink是面向数据流处理和批处理的分布式开源计算框架.2016年阿里巴巴引入flink框架,改造为blink,将其运用到搜索及推荐的离线实时计算中,成功解决了搜索.推荐实时大数据量计算的痛点 ...
- 【认证与授权】2、基于session的认证方式
这一篇将通过一个简单的web项目实现基于Session的认证授权方式,也是以往传统项目的做法. 先来复习一下流程 用户认证通过以后,在服务端生成用户相关的数据保存在当前会话(Session)中,发给客 ...
- amba H2平台用PWM控制LCD背光
ambarella H2系列Soc的GPIO口能作PWM使用的个数有限(GPIO0-GPIO3),从PRM里GPIO: Function Selection章节可以得到如何配置GPIO为PWM功能. ...