704B - Ant Man
来源:互联网 发布:头条科技工作 知乎 编辑:程序博客网 时间:2024/06/06 15:01
题目大意:给你n个点的横坐标,及其相应的4个相关值。给定起点和终点,可以任意跳跃,要求每个点只能走一次,并且从起点走到终点。求消耗的最小值。
由两种消耗
|xi - xj| + ci + bj if j < i
|xi - xj| + di + aj otherwise (j > i)
范围:5000个点
题解:由于考虑顺序的话复杂度太大而且无法有效地控制终点位置。所以可以把此题看做一个最短路问题,在S->E的路径上不断加点,每次记下最小消耗的节点,正确性可以由最短路算法推导。
在路径上不断加点,十分经典的处理方法。
#include<bits/stdc++.h>using namespace std;#define maxn 5100int n,s,e;int x[maxn],a[maxn],b[maxn];int c[maxn],d[maxn],next[maxn];long long f(int i,int j){ long long tmp=abs(x[i]-x[j]); if(i<j) tmp+=d[i]+a[j]; else tmp+=c[i]+b[j]; return tmp;}int main(){ scanf("%d%d%d",&n,&s,&e); for(int i=1;i<=n;i++) scanf("%d",&x[i]); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) scanf("%d",&b[i]); for(int i=1;i<=n;i++) scanf("%d",&c[i]); for(int i=1;i<=n;i++) scanf("%d",&d[i]); next[s]=e; long long tmp,ans,cnt; ans=f(s,e); for(int i=1;i<=n;i++) { if(i==s||i==e) continue; cnt=0x7f7f7f7f7f7f7f7fLL; int pos=0; for(int j=s;j!=e;j=next[j]) { tmp=f(j,i)+f(i,next[j])-f(j,next[j]); if(tmp<cnt) { cnt=tmp; pos=j; } } ans+=cnt; next[i]=next[pos]; next[pos]=i; } cout<<ans<<endl; return 0;}
0 0
- 704B - Ant Man
- 【贪心】Codeforces 704B & 705D Ant Man
- Codeforces Round #366 (Div. 1) B. Ant Man (贪心)
- 贪心+链——Ant Man(Codeforces Round #366 (Div. 1) B )
- codeforces ant man 366 div1b
- [codeforces704B]ant man 解题报告
- codeforces705B B. Spider Man
- B. Spider Man
- Codeforces-705B-Spider Man
- Coderforces #366 B Spider Man
- CodeForces 705B Spider Man
- Codeforces 705B.Spider Man
- Codeforces 705D Ant Man DP(贡献)
- codeforces 705 B. Spider Man (思维)
- 【模拟】Codeforces 705B Spider Man
- Codeforces Round #366 (Div. 1):Ant Man(贪心)
- Codeforces Round #366 (Div. 2) [D] Ant Man
- Codeforces Round #366 (Div. 1):Ant Man(贪心)★
- POJ 2387 Til the Cows Come Home
- 经验分享之在linux虚拟机上实现svn的备份还原
- 【Android移动开发技术】最常用的GitHub上优秀的 Android 开源项目
- 【白话设计模式一】代理模式(Proxy)
- 最短路 ( SPFA )——Ramzi ( Gym 101061 C )
- 704B - Ant Man
- SSH-Hibernate常见配置1
- Android 安卓 4.4 沉浸式状态栏,导航栏
- 剑指offer相关代码
- ZeroMQ编译安装
- 如何用按钮控制循环HorizontalScrollView水平滑动
- 判断对象是否已死的算法
- CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)
- Genymotion下载成功了