比赛 |
2008haoi模拟训练2 |
评测结果 |
WWWWT |
题目名称 |
公路建设 |
最终得分 |
0 |
用户昵称 |
zqzas |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2008-04-23 11:37:56 |
显示代码纯文本
#include <stdio.h>
#include <string.h>
#define maxn 510
#define maxm 2010
#define inf 30000
typedef struct{
int a;
int b;
int cost;
}Edge;
int n,m,zan[maxn],used[maxm],visit[maxn],isin[maxn],data[maxn][maxn];
double ans;
Edge edge[maxm];
FILE *f1,*f2;
int search(int x,int goal)
{
int i,flag;
for (i=1;i<=n;i++)
{
if (data[x][i]!=0 && visit[i]==0)
{
visit[i]=1;
zan[++zan[0]]=data[x][i];
if (i==goal)
{
return 1;
}
data[i][x]=0;
flag=search(i,goal);
data[i][x]=data[x][i];
if (flag)
return 1;
zan[zan[0]--]=0;
}
}
return 0;
}
void run(void)
{
int i,j,flag,a,b,c,max;
for (i=1;i<=m;i++)
{
fscanf(f1,"%d%d%d",&a,&b,&c);
edge[i].a=a;edge[i].b=b;
edge[i].cost=c;
memset(visit,0,sizeof(visit));
memset(zan,0,sizeof(zan));
if (data[a][b]!=0)
{
if (edge[data[a][b]].cost>edge[i].cost)
{
used[data[a][b]]=0;
used[i]=1;
data[a][b]=i;
data[b][a]=i;
}
}
else
{
data[a][b]=i;
data[b][a]=i;
used[i]=1;
flag=search(a,a);
if (flag)
{
max=0;
edge[max].cost=0;
for (j=1;j<=zan[0];j++)
{
if (edge[zan[j]].cost>=edge[max].cost)
max=zan[j];
}
used[max]=0;
data[edge[max].a][edge[max].b]=0;
data[edge[max].b][edge[max].a]=0;
}
}
ans=0;
memset(isin,0,sizeof(isin));
//check
if (i<n-1)
{
fprintf(f2,"0\n");
}
else
{
for (j=1;j<=i;j++)
{
if (used[j])
{
ans+=edge[j].cost;
}
}
ans=ans/2;
fprintf(f2,"%.1lf",ans);
/*for (j=1;j<=i;j++)
{
if (used[j])
fprintf(f2," %d",j);
}
*/
fprintf(f2,"\n");
}
}
}
void ini(void)
{
fscanf(f1,"%d%d",&n,&m);
}
int main(void)
{
f1=fopen("road.in","r");
f2=fopen("road.out","w");
ini();
run();
fclose(f1);fclose(f2);
return 0;
}