1003: [ZJOI2006]物流运输trans

Description

很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系。某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球。这些星球通过特殊的以太隧道互相直接或间接地连接。 但好景不长,很快帝国又重新造出了他的超级武器。凭借这超级武器的力量,帝国开始有计划地摧毁反抗军占领的星球。由于星球的不断被摧毁,两个星球之间的通讯通道也开始不可靠起来。现在,反抗军首领交给你一个任务:给出原来两个星球之间的以太隧道连通情况以及帝国打击的星球顺序,以尽量快的速度求出每一次打击之后反抗军占据的星球的连通快的个数。(如果两个星球可以通过现存的以太通道直接或间接地连通,则这两个星球在同一个连通块中)。
Input

输入文件第一行包含两个整数,N (1 <= N <= 2M) 和M (1 <= M <= 200,000),分别表示星球的数目和以太隧道的数目。星球用0~N-1的整数编号。接下来的M行,每行包括两个整数X, Y,其中(0<=X<>Y
Output

输出文件的第一行是开始时星球的连通块个数。接下来的N行,每行一个整数,表示经过该次打击后现存星球的连通块个数。
Sample Input
8 13
0 1
1 6
6 5
5 0
0 6
1 2
2 3
3 4
4 5
7 1
7 2
7 6
3 6
5
1
6
3
5
7
Sample Output
1
1
1
2
3
3

额,不会写,看题解

先预处理出sp[l,r]表示l到r天都能用的最短路

然后dp,f[i,j]表示i到j天的最小花费

f[i,j]=min(f[i,k]+cost+f[k+1,j])

  1 var
  2     sp:array[0..100,0..100]of longint;
  3     a:array[0..20,0..100]of boolean;
  4     first,next,last,w,q:array[0..10000]of longint;
  5     dis:array[0..20]of longint;
  6     flag,can:array[0..20]of boolean;
  7     n,m,k,e,tot,l,r,head,tail:longint;
  8 
  9 function min(x,y:longint):longint;
 10 begin
 11     if x<y then exit(x);
 12     exit(y);
 13 end;
 14 
 15 procedure spfa;
 16 var
 17     i,j:longint;
 18 begin
 19     head:=1;
 20     tail:=1;
 21     q[1]:=1;
 22     for i:=2 to m do
 23       dis[i]:=21470000;
 24     dis[1]:=0;
 25     for i:=2 to m do
 26       flag[m]:=false;
 27     flag[1]:=true;
 28     for i:=1 to m do
 29       begin
 30         can[i]:=true;
 31         for j:=l to r do
 32           if a[i,j] then can[i]:=false;
 33       end;
 34     while head<=tail do
 35       begin
 36         i:=first[q[head]];
 37         while i<>0 do
 38           begin
 39             if can[last[i]] then
 40             if dis[last[i]]>dis[q[head]]+w[i] then
 41             begin
 42               dis[last[i]]:=dis[q[head]]+w[i];
 43               if flag[last[i]]=false then
 44               begin
 45                 inc(tail);
 46                 flag[last[i]]:=true;
 47                 q[tail]:=last[i];
 48               end;
 49             end;
 50             i:=next[i];
 51           end;
 52         flag[q[head]]:=false;
 53         inc(head);
 54       end;
 55     sp[l,r]:=dis[m];
 56 end;
 57 
 58 procedure insert(x,y,z:longint);
 59 begin
 60     inc(tot);
 61     last[tot]:=y;
 62     next[tot]:=first[x];
 63     first[x]:=tot;
 64     w[tot]:=z;
 65 end;
 66 
 67 procedure init;
 68 var
 69     i,j,x,y,z:longint;
 70 begin
 71     read(n,m,k,e);
 72     for i:=1 to e do
 73       begin
 74         read(x,y,z);
 75         insert(x,y,z);
 76         insert(y,x,z);
 77       end;
 78     read(e);
 79     for i:=1 to e do
 80       begin
 81         read(x,y,z);
 82         for j:=y to z do
 83           a[x,j]:=true;
 84       end;
 85     for l:=1 to n do
 86       for r:=l to n do
 87         spfa;
 88 end;
 89 
 90 var
 91     f:array[0..100,0..100]of longint;
 92 
 93 procedure work;
 94 var
 95     i,j,l:longint;
 96 begin
 97     for i:=1 to n do
 98       for j:=i to n do
 99         f[i,j]:=sp[i,j]*(j-i+1);
100     for i:=1 to n-1 do
101       for j:=1 to n-i do
102         for l:=j to i+j-1 do
103           f[j,i+j]:=min(f[j,i+j],f[j,l]+k+f[l+1,i+j]);
104     write(f[1,n]);
105 end;
106 
107 begin
108     init;
109     work;
110 end.
View Code
原文地址:https://www.cnblogs.com/Randolph87/p/3605047.html