1. 程式人生 > 其它 >Great Graphs(小貪心)

Great Graphs(小貪心)

思路: 貪心 ,以邊為單位看被用了幾次。

本題自己的錯誤: 沒有把題的思路想完全,只想了一部分,只想到了到終點,而沒有到其它點也可以有邊。

Farmer John has a farm that consists of nn pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where
the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction. Unfortunately, Farmer John lost the map of the farm. All he remembers
is an array dd , where d_id i ​ is the smallest amount of time it took the cows to reach the ii -th pasture from pasture 11 using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. 輸入格式 The first line contains one integer tt (
1 \leq t \leq 10^41≤t≤10 4 ) — the number of test cases. Then tt cases follow. The first line of each test case contains a single integer nn ( 1 \leq n \leq 10^51≤n≤10 5 ) — the number of pastures. The second line of each test case contains nn space separated integers d_1, d_2, \ldots, d_nd 1 ​ ,d 2 ​ ,…,d n ​ ( 0 \leq d_i \leq 10^90≤d i ​ ≤10 9 ) — the array dd . It is guaranteed that d_1 = 0d 1=0 . It is guaranteed that the sum of nn over all test cases does not exceed 10^510 5 . 輸出格式 For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. 題意翻譯 給你一個長度為 nn 的序列 \{d\}{d},你需要構造一個有向帶權圖,使得點 11 到點 ii 的最短路長度為 d_id i ​ ,同時使得所有邊的邊權之和儘可能地小。 注意:圖中不能出現負環和重邊。 n\le 10^5,0\le d_i\le 10^9n≤10 5 ,0≤d i ​ ≤10 9 輸入輸出樣例 輸入 #1複製 3 3 0 2 3 2 0 1000000000 1 0 輸出 #1複製 -3 0 0 說明/提示 In the first test case, you can add roads from pasture 11 to pasture 22 with a time of 22 , from pasture 22 to pasture 33 with a time of 11 , from pasture 33 to pasture 11 with a time of -33 , from pasture 33 to pasture 22 with a time of -11 , from pasture 22 to pasture 11 with a time of -22 . The total cost is 2 + 1 + -3 + -1 + -2 = -32+1+−3+−1+−2=−3 .In the second test case, you can add a road from pasture 11 to pasture 22 with cost 10000000001000000000 and a road from pasture 22 to pasture 11 with cost -10000000001000000000 . The total cost is 1000000000 + -1000000000 = 01000000000+−1000000000=0 . In the third test case, you can't add any roads. The total cost is 00 .
View Problem
#include <bits/stdc++.h>
using namespace std;
#define ri register int
#define M 100005

template <class G>void read(G &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return ;
 } 

long long  p[M];
int n,m;
int main()
{
    int T;
    read(T);
    while(T--)
    {
        read(n);
        for(ri i=1;i<=n;i++)
        {
            read(p[i]);
        }
        sort(p+1,p+1+n);
        long long ans=0;
        for(ri i=1;i<n;i++)
        {
            ans-=1ll*(i)*(n-i)*(p[i+1]-p[i]);
        }
        ans+=p[n];
        printf("%lld\n",ans);
    }
    return 0;
    
    
    
    
 } 
View Code