题意:给定n个节点和m条连线,每个节点和每条连线都有一定的权值,如果有一个人取得了一条连线的两个节点,则他获得这条连线的权值,问按照最优策略,两个人的差是多少
解题思路:把桥的权值分到两个节点上去,对答案没有影响
解题代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <string.h> 2 #include <stdlib.h> 3 #include <stdio.h> 4 const int maxn = 111111; 5 long long a[maxn]; 6 int cmp(const void *a , const void * b) 7 { 8 if( *(long long *)a > *(long long *)b) 9 return -1 ; 10 if(*(long long *)a == *(long long *)b ) 11 return 0 ; 12 if(*(long long *)a < *(long long *)b) 13 return 1; 14 } 15 int main() 16 { 17 int n , m ; 18 //freopen("1005.in","r",stdin); 19 20 while(scanf("%d %d",&n,&m) != EOF) 21 { 22 memset(a,0,sizeof(a)); 23 for(int i = 1;i <= n;i ++){ 24 scanf("%I64d",&a[i]); 25 a[i] = a[i]* 2; 26 } 27 int t1,t2; 28 long long t3; 29 for(int i = 1;i <= m;i ++) 30 { 31 scanf("%d %d %I64d",&t1,&t2,&t3); 32 a[t1] += t3; 33 a[t2] += t3; 34 } 35 36 qsort(a+1,n,sizeof(long long ),cmp); 37 long long sum = 0 ; 38 int p = 0 ; 39 for(int i = 1;i <= n; ) 40 { 41 sum += (a[i] - a[i+1]); 42 i = i + 2; 43 44 } 45 printf("%I64d\n",sum/2); 46 } 47 return 0 ; 48 }
没有梦想,何谈远方