4152: [AMPPZ2014]The Captain
4152: [AMPPZ2014]The Captain
Time Limit: 20 Sec Memory Limit: 256 MB
Submit: 1561 Solved: 620
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
2 2
1 1
4 5
7 1
6 7
Sample Output
code
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define mp(a,b) make_pair(a,b)
#define pa pair<long long ,int>
using namespace std; typedef long long LL;
const int N = ;
const LL INF = 1e18; struct Node {
int x,y,id;
}d[N];
int head[N],L,R,tot,n;
bool vis[N];
struct Edge{
int to,nxt,w;
}e[];
long long dis[N];
priority_queue< pa,vector<pa>,greater<pa> >q; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch = nc()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = nc()) x = x * + ch - '';
return x * f;
}
bool cmp1(Node a,Node b) {
return a.x < b.x;
}
bool cmp2(Node a,Node b) {
return a.y < b.y;
}
void add_edge(int u,int v,int w) {
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
e[++tot].to = u;e[tot].w = w;e[tot].nxt = head[v];head[v] = tot;
}
void dij() {
for (int i=; i<=n; ++i) dis[i] = INF;
L = ;R = ;
q.push(mp(,));
dis[] = ;
while (!q.empty()) {
pa x = q.top();q.pop();
int u = x.second;
if (vis[u]) continue;vis[u] = true;
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;;
if (dis[v]>dis[u]+w) {
dis[v] = dis[u] + w;
q.push(mp(dis[v],v));
}
}
}
}
int main() {
freopen("1.txt","r",stdin);
n = read();
for (int i=; i<=n; ++i)
d[i].x = read(),d[i].y = read(),d[i].id = i;
sort(d+,d+n+,cmp1);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].x-d[i].x);
sort(d+,d+n+,cmp2);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].y-d[i].y);
dij();
printf("%d",dis[n]);
return ;
}
spfa被卡了QwQ
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue> using namespace std; typedef long long LL;
const int N = ;
const LL INF = 1e18; struct Node {
int x,y,id;
}d[N];
int head[N],L,R,tot,n;
bool vis[N];
struct Edge{
int to,nxt,w;
}e[];
long long dis[N];
queue<int>q; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch = nc()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = nc()) x = x * + ch - '';
return x * f;
}
bool cmp1(Node a,Node b) {
return a.x < b.x;
}
bool cmp2(Node a,Node b) {
return a.y < b.y;
}
void add_edge(int u,int v,int w) {
e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
e[++tot].to = u;e[tot].w = w;e[tot].nxt = head[v];head[v] = tot;
}
void spfa() {
for (int i=; i<=n; ++i) dis[i] = INF;
L = ;R = ;
q.push();
dis[] = ;
vis[] = true;
while (!q.empty()) {
int u = q.front();q.pop();
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,w = e[i].w;;
if (dis[v]>dis[u]+w) {
dis[v] = dis[u] + w;
if (!vis[v])q.push(v),vis[v] = true;
}
}
vis[u] = false;
}
}
int main() {
n = read();
for (int i=; i<=n; ++i)
d[i].x = read(),d[i].y = read(),d[i].id = i;
sort(d+,d+n+,cmp1);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].x-d[i].x);
sort(d+,d+n+,cmp2);
for (int i=; i<n; ++i)
add_edge(d[i].id,d[i+].id,d[i+].y-d[i].y);
spfa();
printf("%d",dis[n]);
return ;
}
4152: [AMPPZ2014]The Captain的更多相关文章
- 循环队列+堆优化dijkstra最短路 BZOJ 4152: [AMPPZ2014]The Captain
循环队列基础知识 1.循环队列需要几个参数来确定 循环队列需要2个参数,front和rear 2.循环队列各个参数的含义 (1)队列初始化时,front和rear值都为零: (2)当队列不为空时,fr ...
- BZOJ 4152: [AMPPZ2014]The Captain( 最短路 )
先按x排序, 然后只有相邻节点的边才有用, 我们连起来, 再按y排序做相同操作...然后就dijkstra ---------------------------------------------- ...
- 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2107 Solved: 820[Submi ...
- bzoj 4152[AMPPZ2014]The Captain
bzoj 4152[AMPPZ2014]The Captain 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. ...
- BZOJ 4152: [AMPPZ2014]The Captain Dijkstra+贪心
Code: #include <queue> #include <cstdio> #include <cstring> #include <algorithm ...
- bzoj4152[AMPPZ2014]The Captain 最短路
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1517 Solved: 603[Submi ...
- BZOJ4152 AMPPZ2014 The Captain 【最短路】【贪心】*
BZOJ4152 AMPPZ2014 The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点 ...
- 【BZOJ4152】[AMPPZ2014]The Captain 最短路
[BZOJ4152][AMPPZ2014]The Captain Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1 ...
- BZOJ4152:[AMPPZ2014]The Captain——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4152 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1 ...
随机推荐
- 《Head First 设计模式》之适配器模式与外观模式
适配器模式(Adapter) 适配器(adapter-pattern):将一个类的接口,转换成客户期望的另一个接口.适配器让原来接口不兼容的类可以合作无间.两种形式: 对象适配器(组合) 类适配器(多 ...
- 会话跟踪之Session
Session是服务端使用记录客户端状态的一种机制,Session使用简单,但是和Cookie相比,增加了服务器的存储压力[因为为了追求速度,服务器将Session放置在了内存中].Cookie是保存 ...
- while循环,break和continue,运算符,格式化输出
一丶while循环 while条件: 代码块(循环体) #数数 打印1-100 count = 1 while count <= 100: print(count) count += 1 执行顺 ...
- Hibernate笔记3--多表操作-导航查询
一.一对多操作 1.构造实体类及编写配置文件: 一方: // 一个Customer对应多个linkman private Set<Linkman> linkmans = new ...
- 使用SAP云平台 + JNDI访问Internet Service
以Internet Service http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Walldorf&destin ...
- 如何修改集群的公网信息(包括 VIP) (文档 ID 1674442.1)
适用于: Oracle Database - Enterprise Edition - 版本 11.2.0.3 到 12.1.0.2 [发行版 11.2 到 12.1]本文档所含信息适用于所有平台 用 ...
- oc语言特性
It’s a superset of the C programming language and provides object-oriented capabilities and a dynami ...
- Gym - 100676H Capital City(边强连通分量 + 树的直径)
H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...
- Portal简介
Portal 在英语中是入口的意思.Portal 认证通常也称为 Web 认证,一般将 Portal 认 证网站称为门户网站. 未认证用户上网时,设备强制用户登录到特定站点,用户可以免费访问其中的服务 ...
- java对集合的操作,jxl操作excel
http://www.cnblogs.com/epeter/p/5648026.html http://blog.sina.com.cn/s/blog_6145ed810100vbsj.html