codeforces 704B

 1 codeforces 704B - Ant Man 贪心
 2 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr
 3 向右跳:abs(b.x-a.x)+a.lr+b.rl,遍历完所有的点,问你最后的花费是多少
 4 思路:每次选一个点的时候,在当前确定的每个点比较一下,选最短的距离。
 5 为什么可以贪心?应为答案唯一,那么路径必定是唯一的,每个点所在的位置也一定是最短的。
 6 
 7 
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 #define LL long long
11 const int inf = 0x3f3f3f3f;
12 const int MOD =998244353;
13 const int N =200010;
14 #define clc(a,b) memset(a,b,sizeof(a))
15 const double eps = 1e-7;
16 void fre() {freopen("in.txt","r",stdin);}
17 void freout() {freopen("out.txt","w",stdout);}
18 inline int read() {int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}
19 
20 struct node{
21     LL x;
22     LL rl,rr,ll,lr;
23 }p[5010];
24 
25 LL fun(node a,node b){
26     if(b.x<a.x) return abs(b.x-a.x)+a.ll+b.rr;
27     return abs(b.x-a.x)+a.lr+b.rl;
28 }
29 LL net[5010];
30 int  main(){
31     int n,a,b;
32     scanf("%d%d%d",&n,&a,&b);
33     for(int i=1;i<=n;i++) scanf("%I64d",&p[i].x);
34     for(int i=1;i<=n;i++) scanf("%I64d",&p[i].rl);
35     for(int i=1;i<=n;i++) scanf("%I64d",&p[i].rr);
36     for(int i=1;i<=n;i++) scanf("%I64d",&p[i].ll);
37     for(int i=1;i<=n;i++) scanf("%I64d",&p[i].lr);
38     LL tem,sum,ans=0;
39     int k;
40     net[a]=b;
41     ans+=fun(p[a],p[b]);
42     for(int i=1;i<=n;i++){ 
43         if(i==a||i==b) continue;
44         sum=1e18;
45         for(int j=a;j!=b;j=net[j]){
46             tem=fun(p[j],p[i])+fun(p[i],p[net[j]])-fun(p[j],p[net[j]]);
47             if(tem<sum){
48                 sum=tem;
49                 k=j;
50             }
51         }
52         ans+=sum;
53         net[i]=net[k];
54         net[k]=i;
55     }
56     printf("%I64d
",ans);
57     return 0;
58 }
原文地址:https://www.cnblogs.com/ITUPC/p/5764606.html