Penguin-Avia
Time limit: 1.0 second
Memory limit: 64 MB
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.
However, not everything is that easy in Antarctica. For cancelling a flight, the airline must pay a one-time forfeit of d Antarctic dollars. To obtain slots for a new flight, the company must spend a Antarctic dollars to grease the palm of the godfather of the Antarctic mafia nicknamed Walrus.
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 |
7 |
Problem Author: Alexander Ipatov
【分析】给出一个图,双向的,现要你删除,添加一些边,使得所有点联通,删除,添加都需要费用,问你最小费用是多少。就是个简 单的并查集问题。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
char w[N][N];
int vis[N][N],VIS[N][N];
int n,m,k,c;
ll s=;
int parent[N];
int Find(int x)
{
if(parent[x]!=x)parent[x]=Find(parent[x]);
return parent[x];
}
void Union(int x,int y)
{
x=Find(x);y=Find(y);
if(x==y)return;
parent[y]=x;
}
int main()
{
for(int i=;i<=;i++)parent[i]=i;
int a,d;
scanf("%d%d%d",&n,&a,&d);
for(int i=;i<=n;i++){
scanf("%s",w[i]+);
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(w[i][j]==''){
int x=Find(i);
int y=Find(j);
if(x==y)vis[i][j]=vis[j][i]=,s+=a;
else Union(i,j);
}
}
}
int ok=Find();
for(int i=;i<=n;i++){
int x=Find(i);
if(x!=ok&&vis[][i]!=)vis[][i]=vis[i][]=,s+=d,Union(,i);
}
printf("%lld\n",s);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
int x=Find(i);
int y=Find(j);
if(!vis[i][j])printf("");
else if(vis[i][j]==)printf("d");
else if(vis[i][j]==)printf("a");
}
printf("\n");
}
return ;
}