Battle over Cities
来源:互联网 发布:西安软件新城二期商铺 编辑:程序博客网 时间:2024/05/17 23:35
Battle over Cities
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 0
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Given the map of cities which have all the destroyed and remaining highways marked, you are supposed to tell the cost to connect other cities if each city is conquered by the enemy.
Input
Note: It is guaranteed that the whole country was connected before the war and there is no duplicated high ways between any two cities.
Output
Sample Input
34 51 2 1 11 3 1 12 3 1 02 4 1 13 4 2 04 51 2 1 11 3 1 12 3 1 02 4 1 13 4 1 03 21 2 1 11 3 1 1
Sample Output
12001100inf00
Author
Source
#include<iostream>
#include<stdio.h>
#include<algorithm>
#define MAXN 20001
using namespace std;
int city,load;
int i,j;
struct edge
{
int u;
int v;
int cost;
int destory;
}edges[MAXN];
int parent[MAXN];
void UFset()
{
for(j=1;j<=city;j++)
{
parent[j]=-1;
}
}
int Find(int x)
{
int s;
for(s=x;parent[s]>=0;s=parent[s]);
while(s!=x)
{
int tmp=parent[x];
parent[x]=s;
x=tmp;
}
return s;
}
void Union(int R1,int R2)
{
int r1=Find(R1);
int r2=Find(R2);
int tmp=parent[r1]+parent[r2];
if(parent[r1]>parent[r2])
{
parent[r1]=r2;
parent[r2]=tmp;
}
else
{
parent[r2]=r1;
parent[r1]=tmp;
}
}
int cmp(const void*a,const void*b)
{
edge aa=*(const edge*)a;
edge bb=*(const edge*)b;
return aa.cost-bb.cost;
}
void Kruskal(int t)
{
int sumweight=0;
int num=0;
int u,v;
UFset();
for(j=0;j<load;j++)
{
u=edges[j].u;
v=edges[j].v;
if(u==t || v==t)
continue;
if(Find(u)!=Find(v))
{
sumweight+=edges[j].cost;
num++;
Union(u,v);
}
// if(num>=city-1)
// break;
}
if(num==city-2)
printf("%d\n",sumweight);
else
printf("inf\n");
}
int main()
{
int n;
int u,v,cost,destory;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&city,&load);
for(j=0;j<load;j++)
{
scanf("%d%d%d%d",&u,&v,&cost,&destory);
edges[j].u=u;
edges[j].v=v;
if(destory==1)
edges[j].cost=0;
else
edges[j].cost=cost;
}
qsort(edges,load,sizeof(edges[0]),cmp);
int t;
for(t=1;t<=city;t++)
{
Kruskal(t);
}
}
return 0;
}
- Battle over Cities
- Battle Over Cities
- 1013. Battle Over Cities
- 1013. Battle Over Cities
- PAT_1013: Battle Over Cities
- 1013. Battle Over Cities
- PAT1013. Battle Over Cities
- [PAT]Battle Over Cities
- 1013.Battle Over Cities
- PAT_A_1013 Battle Over Cities
- pat1013 Battle Over Cities
- 1013. Battle Over Cities
- 1013. Battle Over Cities
- 1013. Battle Over Cities
- 1013. Battle Over Cities
- Battle Over Cities
- 1013. Battle Over Cities
- 1013. Battle Over Cities
- 线性表的链式存储
- 用VS2010建立解决方案,用于生成多个Dll的项目
- Windows 下的符号链接(软链接)为金山快盘添加同步文件夹
- 打印一个菱形,用其他字符填充空白区域
- Linux-u-boot
- Battle over Cities
- 快速排序
- java30个目标
- ARM+LINUX移植攻略(十三)一些需要注意的问题
- XPO学习
- 数据库设计建议
- U-boot 链接加载
- 用命令行启动、停止mysql服务
- 责任链模式