Sgu-218 Unstable Systems
Question:
Sasha is the administrator of a network composed of N computers. There are now N programs required to be assigned to these computers for running. Due to machine instability, each computer has an "error value" for different programs (proportional to the probability of running errors ). Now you are asked to help Sasha arrange these computers to run programs to minimize the maximum value among all "error values. Input n to you and then a matrix of n * n. line I indicates the error value of the program running on the computer I. Then you need to output the minimum error value and then output the program corresponding to each computer.
Solution:
First, I thought this question was a DP question, and then I thought I could find a second answer.
Specific practice: first, we have a binary answer (note that the error value may be negative, indicating that the error is miserable), and then we have a very simple binary graph matching, when the edge weight exceeds your current MAX value, this edge cannot match. Then we only need to find a set of complete matches.
AC code:
#include
#include
#include
#include
#include
#include #define MAX(a,b) ((a)>(b)?(a):(b))#define MIN(a,b) ((a)>(b)?(b):(a))using namespace std;int cost[510][510]={{0}};int n;int Max=-2e9;int Min=2e9;int ans=0;int g;int hash[510]={0};int father[510]={0};int son[510]={0};int find(int k){for(int i=1;i<=n;i++){if(cost[k][i]>g) continue;if(hash[i]==1) continue;if(father[i]==k) continue;hash[i]=1;if(father[i]==0 || find(father[i])==1){father[i]=k;son[k]=i;return 1;}}return 0;}int check(){int s=0;memset(father,0,sizeof(father));memset(son,0,sizeof(son));for(int i=1;i<=n;i++){memset(hash,0,sizeof(hash));if(find(i)==1)s++;}return s==n;}int main(){int ason[510]={0};scanf("%d",&n);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){scanf("%d",&cost[i][j]);Max=MAX(Max,cost[i][j]);Min=MIN(Min,cost[i][j]);}for(int l=Min,r=Max;l<=r;){g=(l+r)>>1;if(check()==1){ans=g;r=g-1;memcpy(ason,son,sizeof(son));}else l=g+1;}cout<