随笔 - 540  文章 - 0 评论 - 39 阅读 - 12万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

这道题没弄明白

初始模型很好想,是用到了最小生成树的性质

加入非树边后树上形成的环,非树边一定大于等于任意树边

然后考虑树边一定是缩小,非树边一定是增大

有di+wi>=dj-wj wi+wj>=dj-di(j是加入i形成的环上的边)

然后不知道为什么求∑wi最小就是跑最大费用可行流

求神犇指教

复制代码
  1 type node=record
  2        po,next,cost,flow:longint;
  3      end;
  4 
  5 var e:array[0..2000010] of node;
  6     v,f:array[0..810] of boolean;
  7     a:array[0..60,0..60] of longint;
  8     d,pre,cur,w,p:array[0..810] of longint;
  9     q:array[0..2000010] of longint;
 10     x,y,len,i,j,n,m,t,k:longint;
 11 
 12 procedure add(x,y,f,c:longint);
 13   begin
 14     inc(len);
 15     e[len].po:=y;
 16     e[len].next:=p[x];
 17     e[len].cost:=c;
 18     e[len].flow:=f;
 19     p[x]:=len;
 20   end;
 21 
 22 procedure build(x,y,f,c:longint);
 23   begin
 24     add(x,y,f,c);
 25     add(y,x,0,-c);
 26   end;
 27 
 28 function dfs(x:longint):boolean;
 29   var i:longint;
 30   begin
 31     f[x]:=true;
 32     if x=j then
 33     begin
 34       q[0]:=-1;
 35       exit;
 36     end;
 37     for i:=1 to n do
 38       if not f[i] and v[a[x,i]] then
 39       begin
 40         inc(t);
 41         q[t]:=a[x,i];
 42         dfs(i);
 43         if q[0]=-1 then exit;
 44         q[t]:=0;
 45         dec(t);
 46       end;
 47   end;
 48 
 49 function spfa:boolean;
 50   var f,r,y,i,j,x:longint;
 51   begin
 52     f:=1;
 53     r:=1;
 54     q[1]:=0;
 55     d[0]:=0;
 56     for i:=1 to t do
 57       d[i]:=-1000007;
 58     fillchar(v,sizeof(v),false);
 59     while f<=r do
 60     begin
 61       x:=q[f];
 62       v[x]:=false;
 63       i:=p[x];
 64       while i<>-1 do
 65       begin
 66         y:=e[i].po;
 67         if e[i].flow>0 then
 68           if d[y]<d[x]+e[i].cost then
 69           begin
 70             d[y]:=d[x]+e[i].cost;
 71             pre[y]:=x;
 72             cur[y]:=i;
 73             if not v[y] then
 74             begin
 75               v[y]:=true;
 76               inc(r);
 77               q[r]:=y;
 78             end;
 79           end;
 80         i:=e[i].next;
 81       end;
 82       inc(f);
 83     end;
 84     if d[t]>=0 then exit(true) else exit(false);
 85   end;
 86 
 87 function maxcost:longint;
 88   var i,j:longint;
 89   begin
 90     maxcost:=0;
 91     while spfa do
 92     begin
 93       i:=t;
 94       while i<>0 do
 95       begin
 96         j:=cur[i];
 97         dec(e[j].flow);
 98         inc(e[j xor 1].flow);
 99         i:=pre[i];
100       end;
101       maxcost:=maxcost+d[t];
102     end;
103   end;
104 
105 begin
106   len:=-1;
107   fillchar(p,sizeof(p),255);
108   readln(n,m);
109   for i:=1 to m do
110   begin
111     readln(x,y,w[i]);
112     a[x,y]:=i;
113     a[y,x]:=i;
114   end;
115   for i:=1 to n-1 do
116   begin
117     readln(x,y);
118     k:=a[x,y];
119     v[k]:=true;
120     build(0,k,1,0);
121   end;
122   for i:=1 to n do
123     for j:=i+1 to n do
124       if (a[i,j]>0) and not v[a[i,j]] then
125       begin
126         build(a[i,j],m+1,1,0);
127         fillchar(f,sizeof(f),false);
128         t:=0; q[0]:=0;
129         dfs(i);
130         for k:=1 to t do
131           if w[q[k]]>w[a[i,j]] then build(q[k],a[i,j],1,w[q[k]]-w[a[i,j]]);
132       end;
133   t:=m+1;
134   writeln(maxcost);
135 end.
View Code
复制代码

 

posted on   acphile  阅读(214)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
· 重磅发布!DeepSeek 微调秘籍揭秘,一键解锁升级版全家桶,AI 玩家必备神器!
点击右上角即可分享
微信分享提示