PAT TOP 1001 Battle Over Cities - Hard Version (35)_kircher的博客-程序员宅基地

技术标签: PAT TOP  

问题描述:

1001 Battle Over Cities - Hard Version (35 分)

It is vitally important to have all the cities connected by highways in a war. If a city is conquered by the enemy, all the highways from/toward that city will be closed. To keep the rest of the cities connected, we must repair some highways with the minimum cost. On the other hand, if losing a city will cost us too much to rebuild the connection, we must pay more attention to that city.

Given the map of cities which have all the destroyed and remaining highways marked, you are supposed to point out the city to which we must pay the most attention.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N (≤500), and M, 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 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.

Output Specification:

For each test case, just print in a line the city we must protest the most, that is, it will take us the maximum effort to rebuild the connection if that city is conquered by the enemy.

In case there is more than one city to be printed, output them in increasing order of the city numbers, separated by one space, but no extra space at the end of the line. In case there is no need to repair any highway at all, simply output 0.

Sample Input 1:

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

Sample Output 1:

1 2

Sample Input 2:

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

Sample Output 2:

0

 

这一题也是关于最小生成树的问题,同样利用并查集+Kruskal算法即可解决。。。

首先,对于所有读入的边,先按照Status排序,再按照Cost排序。然后,对于每个需要保卫的城市,遍历边的集合,对于不含此次城市的边,修改相应并查集的状态,若Status=0且边所连接的两个城市不在一个集合里,则当前城市的费用需要加上这条边的维修费用。最后,遍历每个城市的维修费用,输出相应的城市序号。

AC代码:

#include<bits/stdc++.h>
using namespace std;
struct edge
{
	int c1,c2,ct,st;
	bool operator< (const edge& ee)const
	{
		if(st!=ee.st)
		return st>ee.st;
		else
		return ct<ee.ct;
	}
} e;
vector<int> vc;
inline int fr(int n)
{
	int rn=n;
	for(;vc[rn];rn=vc[rn]);
	if(rn-n)
	vc[n]=rn;
	return rn;
}
int main()
{
//	freopen("data.txt","r",stdin);
	int n,m,c=0,ci=0;
	scanf("%d %d",&n,&m);
	vector<edge> v;
	for(int i=0;i<m;i++)
	{
		scanf("%d %d %d %d",&e.c1,&e.c2,&e.ct,&e.st);
		v.emplace_back(e);
	}
	sort(v.begin(),v.end());
	vector<int> vr(n);
	for(int i=1;i<n+1;i++)
	{
		vc.clear();
		vc.resize(n+1,0);
		int cp=0;
		for(auto j:v)
		{
			if((j.c1-i)&&(j.c2-i))
			{
				int rc1=fr(j.c1);
				int rc2=fr(j.c2);
				if(rc1-rc2)
				{
					vc[max(rc1,rc2)]=min(rc1,rc2);
					if(!j.st) cp+=j.ct;
				}
			}
		}
		
		int flag=0;
		for(int j=1;j<n+1;j++)
		if(j-i && !vc[j]) flag++;
		if(flag-1) cp=10000;
		vr[i-1]=cp;
		if(cp>c)
		{
			c=cp;
			ci=i-1;
		}
	}
	if(c)
	{
		printf("%d",ci+1);
		for(int i=ci+1;i<n;i++)
		{
			if(vr[i]==c)
			printf(" %d",i+1);
		}
	}
	else
	printf("0");
	return 0;
}

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/baidu_37550206/article/details/86668518

智能推荐

深度解密(一):边缘计算的理解与思考_边缘计算叠加中特估_巨子嘉的博客-程序员宅基地

边缘计算范围太大,场景太泛,整个行业都比较缺少经典的案例及标准,尤其是产业边缘计算,在国家层面都有在大力推进产业互联网,但是仍然艰难前行;所以对边缘计算的建设,一定是面向真实的业务场景及期望,整体规划面向价值逐步建设。

windows部署tomcat服务自动启动,同时解决服务无法启动的问题_error: invalid_service: tomcat_starry-night的博客-程序员宅基地

1、安装tomcat服务进入tomcat的bin目录下,运行service.bat install安装。如果提示Failed,可执行services.msc查看服务,看是否已存在Tomcat7服务,有则需要给新安装的服务指定其他名称。注:如果需要删除服务,则使用sc delete 服务名2、进入系统服务启动服务,并将服务设置成自动启动。如果启动失败,报错:windows 不能在【计算器名】启动to...

为图像添加椒盐噪声和高斯噪声_hairuiJY的博客-程序员宅基地

http://blog.csdn.net/qq_34784753/article/details/69379135 下面简单介绍两种图像噪声,即椒盐噪声和高斯噪声。1.椒盐噪声       椒盐噪声也称为脉冲噪声,是图像中经常见到的一种噪声,它是一种随机出现的白点或者黑点,可能是亮的区域有黑色像素或是在暗的区域有白色像素(或是两者皆有)。盐和胡椒噪声的成因可能是

Hadoop 压缩格式_bzip2可切分_天地不仁以万物为刍狗的博客-程序员宅基地

Hadoop应用处理的数据集非常大,因此需要借助于压缩。使用哪种压缩格式与待处理的文件的大小、格式和所使用的工具相关。下面有一些建议,大致是按照效率从高到低排列的。使用容器文件格式,例如顺序文件、Avro数据文件、ORCFiles或者Parquet文件,所有这些文件格式同时支持压缩和切分。通常最好与一个快速压缩工具联合使用,例如LZO,LZ4,或者Snappy。使用支持切分的压缩格式,例如bzip...

Android7.0以后的ninja编译系统_Android系统攻城狮的博客-程序员宅基地

1、Ninja: 用于提高编译速度的编译系统。 可执行文件位于 prebuilts/ninja/linux-x86/ninja2、Kati: 用于把Makefiel转成成ninja file,自身没有编译能力,转换后使用Ninja编译。 源代码位于: build/kati 可执行文件会被生成到: out/host/linux-x86/bin/ckati 使用方法可参考 README...

基于深度学习生成音乐(mid格式)_lmd-matched数据集_Harrytsz的博客-程序员宅基地

摘要之前在看Andrew Ng 的deep learning 视频教程,在RNN 这一节的课后作业里,实现了一个基于deepjazz的music generator,实验之后发现产生的结果还有模有样的,这激发了我的兴趣,于是我就查阅了一些资料,看看音乐的自动生成方面最近有哪些进展,特别是深度学习在这一块的应用。之前在看Andrew Ng 的deep learning 视频教程,在RNN 这一节...

随便推点

重绘CButton控件_Car12的博客-程序员宅基地

1,创建一个类 继承自:CButtonl;#pragma once// CMyButtonclass CMyButton : public CButton{ DECLARE_DYNAMIC(CMyButton)public: CMyButton(); virtual ~CMyButton();protected: DECLARE_MESSAGE_MAP()publ

HDU2039 三角形【水题】_海岛Blog的博客-程序员宅基地

三角形Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 103864    Accepted Submission(s): 34070Problem Description给定三条边,请你判断一下能不能组成一个三角形。 Input输入数据第一...

基于SkyWalking的分布式跟踪系统 - 环境搭建_飘渺Jam的博客-程序员宅基地

是时候关注我们一波了前面的几篇文章我们聊了基于Metrics的监控Prometheus,利用Prometheus和Grafana可以全方位监控你的服务器及应用的性能指标,...

机器学习-概率分布(PRML 第二章总结)_prml 练习答案 2.1_玩世彳不恭的博客-程序员宅基地

概率分布概率分布离散变量1伯努利分布2二项分布3多项式分布连续变量1 beta分布2 狄利克雷分布3 高斯分布极大似然估计最大后验估计贝叶斯估计1.离散变量1.1伯努利分布伯努利分布,进行一次伯努利实验,如投掷一次硬币,x=1x=1代表正面,其概率为μ\mu,x=0x=0代表反面,其概率为1−μ1-\mu。 p(x|μ)=ux(1−u)1−xp(x|\mu)=u^x(1-u

linux查看磁盘io的几种方法__小海_的博客-程序员宅基地

来源:http://www.3lian.com/edu/2013/12-03/112174.html怎样才能快速的定位到并发高是由于磁盘io开销大呢?可以通过三种方式:  第一种:用 top 命令 中的cpu 信息观察  Top可以看到的cpu信息有:  Tasks: 29 total, 1 running, 28 sleeping, 0 stopped,

[472]tf.Variable()函数_周小董的博客-程序员宅基地

tf.Variable(initializer,name),参数initializer是初始化参数,name是可自定义的变量名称,用法如下:import tensorflow as tfv1=tf.Variable(tf.random_normal(shape=[4,3],mean=0,stddev=1),name='v1')v2=tf.Variable(tf.constant(2),na...

推荐文章

热门文章

相关标签