1709. Penguin-Avia
Time limit: 1.0 second Memory limit: 64 MB
The Penguin-Avia airline, along with other Antarctic airlines, experiences financial difficulties because of the world's economic crisis. People of Antarctica economize on flights and use trains or prefer to stay at home. The airline's management hopes that the number of their clients will increase in the summer due to the tourists visiting the coastal resorts. In order to hold out till the summer, it was decided to optimize the flight scheme by cancelling some flights and introducing some new flights.
Director of Penguin-Avia assumes that after the optimization the flight scheme must have the following properties:
- Using one or more Penguin-Avia flights, one can get from any Antarctic airport to any other.
- The scheme must contain the minimal number of flights among all the schemes satisfying the first property.
Help Director of Penguin-Avia transform the existing flight scheme spending as little money as possible. For doing that, you will be presented with a travel card for all flights of the airline.
Input
In the first line you are given the number n of airports in Antarctica, 2 ≤ n ≤ 100. In the second line you are given the integers d and a, 1 ≤ d, a ≤ 106. The following n lines describe the existing scheme of Penguin-Avia flights in the form of an n × n matrix. There is “1” in a cell (i, j) of the matrix if the airline has flights between the airports i and j. Otherwise, there is “0” in the cell. It is guaranteed that the matrix is symmetric and there are only zeros on its diagonal.
Output
In the first line output the minimal amount of money necessary for the optimization of the existing flight scheme. In the next n lines give the plan of changing the scheme in the form of an n × n matrix. A cell (i, j) of this matrix contains the symbol “d” if the flights between the airports i and j should be cancelled. In the case when a new flight should be introduced between these airports, the cell contains the symbol “a”. The remaining cells contain the symbol “0”. The matrix must be symmetric. If there are several optimal schemes, output any one of them.
Sample
input | output |
---|---|
6 2 3 011000 101000 110000 000011 000101 000110 |
7 0d0000 d00000 000a00 00a0d0 000d00 000000 |
Problem Author: Alexander Ipatov Problem Source: The 13th Urals Collegiate Programing Championship, April 04,
************************************************************************************************
并查集&&最小生成树……
************************************************************************************************

1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cmath> 5 #include<cctype> 6 #include<cstdio> 7 #include<stack> 8 #include<queue> 9 #include<vector> 10 using namespace std; 11 int fa[1000]; 12 int i,j,k,n; 13 void make()//数组初始化 14 { 15 for(i=1;i<=n;i++) 16 fa[i]=i; 17 } 18 int father(int i)//并查集查询(找最远祖先,并逐个复制) 19 { 20 if(fa[i]!=i) 21 fa[i]=father(fa[i]); 22 if(fa[i]==i) 23 return i; 24 25 } 26 bool Unon(int i,int j)//合并 27 { 28 i=father(i); 29 j=father(j); 30 if(i==j)return false; 31 else 32 { 33 fa[i]=j; 34 return true; 35 } 36 } 37 char map[105][105]; 38 char c; 39 int main() 40 { 41 int d,a; 42 long long money; 43 cin>>n; 44 cin>>d>>a; 45 46 for(i=1;i<=n;i++) 47 for(j=1;j<=n;j++) 48 map[i][j]='0'; 49 money=0; 50 make(); 51 for(i=1;i<=n;i++) 52 { 53 for(j=1;j<=n;j++) 54 { 55 cin>>c; 56 if(i<j&&c=='1') 57 if(!Unon(i,j))//当第二次为同一集合时,减 58 { 59 money+=d; 60 map[i][j]=map[j][i]='d'; 61 } 62 63 64 } 65 getchar(); 66 } 67 int pre=father(1); 68 int cur; 69 bool flag[1001]; 70 memset(flag,false,sizeof(flag)); 71 flag[pre]=true; 72 for(i=2;i<=n;i++)//前面查并完了,不为同一集合时,加 73 { 74 cur=father(i); 75 if(!flag[cur]) 76 { 77 money+=a; 78 map[pre][cur]=map[cur][pre]='a'; 79 pre=cur; 80 flag[pre]=true; 81 } 82 } 83 cout<<money<<endl; 84 for(i=1;i<=n;i++) 85 { 86 for(j=1;j<=n;j++) 87 cout<<map[i][j]; 88 cout<<endl; 89 } 90 return 0; 91 92 }