点此查看题目
#include <stdio.h>
#include <stdlib.h>
#define DEBUG
#ifndef DEBUG
#define N_MAX (2000+1)
#define M_MAX (10000+1)
#else
#define N_MAX (5+1)
#define M_MAX (10+1)
#endif
int g_iEffectOut = 0;
int g_iCrimeNum = 0,g_iHateCnt = 0;
int g_iEffectA = 0,g_iEffectB = 0;
int g_Hate[N_MAX][N_MAX] = {0};// 仇恨对数最多为n*n >= M_MAX
int g_PrisonA[M_MAX],g_PrisonB[M_MAX];
int g_iCrimeNumA = 0, g_iCrimeNumB = 0;
void ReadFile()
{
int i = 0,j = 0;
FILE *fp = fopen("prison.in","rb");
if (fp == NULL)
{
return;
}
fscanf(fp,"%d%d",&g_iCrimeNum,&g_iHateCnt);
for (int ii=0;ii<g_iHateCnt;ii++)
{
fscanf(fp,"%d%d",&i,&j);
fscanf(fp,"%d",&g_Hate[i][j]);
g_Hate[j][i] = g_Hate[i][j];
}
fclose(fp);
}
void WriteFile()
{
int i = 0;
FILE *fp = fopen("prison.out","wb");
if (fp == NULL)
{
return;
}
fprintf(fp,"%d",g_iEffectOut);
fclose(fp);
}
int CheckA(int iCrimeId)
{
int iRet = g_iEffectA;
for (int i = 0;i<g_iCrimeNumA;i++)
{
if (g_Hate[g_PrisonA[i]][iCrimeId] > g_iEffectA)
{
iRet = g_Hate[g_PrisonA[i]][iCrimeId];
}
}
return iRet;
}
int CheckB(int iCrimeId)
{
int iRet = g_iEffectB;
for (int i = 0;i<g_iCrimeNumB;i++)
{
if (g_Hate[g_PrisonB[i]][iCrimeId] > g_iEffectB)
{
iRet = g_Hate[g_PrisonB[i]][iCrimeId];
}
}
return iRet;
}
void InCrimeA(int iCrimeId,int iEffectNew)
{
for (int i = 0;i<g_iCrimeNumA;i++)
{
if (g_PrisonA[i] == iCrimeId)
{
return;
}
}
g_PrisonA[g_iCrimeNumA] = iCrimeId;
g_iEffectA = iEffectNew;
g_iCrimeNumA ++;
}
void InCrimeB(int iCrimeId,int iEffectNew)
{
for (int i = 0;i<g_iCrimeNumB;i++)
{
if (g_PrisonB[i] == iCrimeId)
{
return;
}
}
g_PrisonB[g_iCrimeNumB] = iCrimeId;
g_iEffectB = iEffectNew;
g_iCrimeNumB ++;
}
void InitPrison()
{
int iMaxi = 0,iMaxj = 0,iMaxEffect = 0;
for (int i = 1;i<=g_iCrimeNum;i++)
{
for (int j = 1;j<=g_iCrimeNum;j++)
{
if (g_Hate[i][j] > iMaxEffect)
{
iMaxEffect = g_Hate[i][j];
iMaxi = i;
iMaxj = j;
}
}
}
InCrimeA(iMaxi,0);
InCrimeB(iMaxj,0);
}
void Group()
{
int iRetA = 0,iRetB = 0;
InitPrison();
for (int i = 1;i<= g_iCrimeNum;i++)
{
iRetA = CheckA(i);
iRetB = CheckB(i);
if (iRetA > iRetB)
{
InCrimeB(i,iRetB);
}
else
{
InCrimeA(i,iRetA);
}
}
}
int main(int argc, char* argv[])
{
ReadFile();
Group();
if (g_iEffectA > g_iEffectB)
{
g_iEffectOut = g_iEffectA;
}
else
{
g_iEffectOut = g_iEffectB;
}
WriteFile();
return 0;
}