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

It is vitally important to have all the cities connected by highways in a war, but some of them are destroyed now because of the war. Furthermore,if a city is conquered, all the highways from/toward that city will be closed by the enemy, and we must repair some destroyed highways to keep other cities connected, with the minimum cost if possible.
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

The input contains multiple test cases. The first line is the total number of cases T (T ≤ 10). Each case starts with a line containing 2 numbers N (0 < N ≤ 20000), and M (0 ≤ M ≤ 100000), which are the total number of cities, and the number of highways, respectively. Then M lines follow, each describes a highway by 4 integers: City1 City2 Cost Status where City1 and City2 are the numbers of the cities the highway connects (the cities are numbered from 1 to N), Cost (0 < Cost ≤ 20000) is the effort taken to repair that highway if necessary, and Status is either 0, meaning that highway is destroyed, or 1, meaning that highway is in use.
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

For each test case, output N lines of integers. The integer in the i-th line indicates the cost to keep the cities connected if the i-th city is conquered by the enemy. In case the cities cannot be connected after the i-th city is conquered by the enemy, output "inf" instead in the corresponding place.

Sample Input

3
4 5
1 2 1 1
1 3 1 1
2 3 1 0
2 4 1 1
3 4 2 0
4 5
1 2 1 1
1 3 1 1
2 3 1 0
2 4 1 1
3 4 1 0
3 2
1 2 1 1
1 3 1 1

Sample Output

1
2
0
0
1
1
0
0
inf
0
0

Author

GUAN, Yao

Source

The 2010 ACM-ICPC Asia Chengdu Regional Contest
错误的代码:供自己以后修改
听说这道题要用割点+最小生成树

#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相关推荐

  1. 1013 Battle Over Cities(并查集解法)

    关于背景的介绍见1013 Battle Over Cities(图的DFS解法) DFS就是不算特定结点后数连通子图的总数,再减一.我想着那么并查集就是数不算特定节点后,集合元素(根)的个数.但是我弄 ...

  2. PAT甲级1013 Battle Over Cities:[C++题解]并查集、结构体存边

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:并查集题目. 不清楚并查集的小伙伴,请移步并查集原理并查集板子:acwing836. 合并集合. 题意:给定一个连通图,当删掉任意1个 ...

  3. PAT TOP 1001 Battle Over Cities - Hard Version (35)

    问题描述: 1001 Battle Over Cities - Hard Version (35 分) It is vitally important to have all the cities c ...

  4. PAT (Advanced Level) Practise 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  5. 1013. Battle Over Cities (25) 连通图

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  6. 【解析】1013 Battle Over Cities (25 分)_31行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 It is vitally important to have all the cities connected by highw ...

  7. 【PAT甲级 - 1013】Battle Over Cities (25分)(并查集)

    题干: It is vitally important to have all the cities connected by highways in a war. If a city is occu ...

  8. PAT1013. Battle Over Cities (25)

    题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...

  9. PAT甲级A1013. Battle Over Cities (25)

    题目描述 It is vitally important to have all the cities connected by highways in a war. If a city is occ ...

最新文章

  1. 到底是先更新数据库还是先更新缓存?
  2. 用正交变换化二次型为标准形的具体步骤
  3. BIOS INT 10中断功能
  4. oracle测试表什么名字,Oracle_PLSQL测试题与答案(绝对经典)
  5. 将 Sidecar 容器带入新的阶段
  6. 【Python】matplotlib可视化必知必会富文本绘制方法
  7. oracle-备份工具exp-imp
  8. Ontology与OO作为一种需求分析或软件构建方法的存在意义
  9. Linux LVM动态扩容
  10. linux 基础命令(三)
  11. 转载:手把手教你把Vim改装成一个IDE编程环境(图文)
  12. 网游中的网络编程3:在UDP上建立虚拟连接
  13. FPGA 二选一数据选择器
  14. matlab 试验设计,试验设计与MATLAB数据分析
  15. 神策分析:三大价值搭建精细化渠道管理体系
  16. F30.2018年版本北大中文核心期刊目录
  17. 华三交换机升级的ipe文件_H3C 交换机升级说明
  18. CSDN:2019年度CSDN博客之星评选竞赛——105号【一个处女座的程序猿】,感谢您,投上的宝贵一票,感谢!感恩!
  19. 19隆冬的倔强(updating)
  20. 开了店铺没访客没流量?Shopee店铺日常运营引流方式来啦

热门文章

  1. 声纹识别常用数据集简介
  2. 【网络】网络模型和实施布线
  3. 饮料罐装生产流水线的plc(西门子S7-200)课程设计(说明书+任务书+接线图+梯形图+流程图)
  4. 便签存储在哪个文件夹,Windows 7便笺保存位置
  5. java接口汽车品牌_根据品牌获取所有车型示例代码
  6. 微型计算机控制技术第一章绪论
  7. html相对定位 不占位置,相对定位与绝对定位的区别 相对定位是以当前位置为基准计算的...
  8. 录屏没声音,本地录屏没声音,解决办法(亲测管用)
  9. Android 仿淘宝2017添加地址
  10. vod_play.html修改播放器页面模板