scoj2832: Mars city fzoj1462

Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

Year 2900. Many people left Earth and built some cities on Mars.

Currently there are N cities, some of which are connected by narrow one-way roads.

The president of Mars has decided to build police stations throughout the Mars. Given the cost of building police stations at each city, we cannot build them at all cities, due to the financial deficit. The only requirement is that every city must be reachable
from some police station by roads. They expect the cost of all stations as low as possible.

 Input

The first line for each case contains two integers n, m(0 < n <= 5,000. 0 < m <= 100,000), the number of cities and roads. The follows n lines each contains two integers, i, c ( 0 < i <= n . 0 < c <=10,000 ), c represents the cost of building a police station
in city i , The next m lines each contains two integers u and v ( 0 < u, v <= n), indicating that there is a road form u to v .

 Output

For each test case, print the minimum cost of stations.

 Sample Input

4 51 53 24 92 61 22 33 41 44 3

 Sample Output

5

 Source

FZU 2006 ICPC Qualification Round I

首先一发强连通分量,然后把入度为0的连通块里最便宜的那个点加到答案里去

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
stack<int>sk;
bool insk[5010];
int step,dfn[5010],low[5010],belong[5010],tol;
struct Edge
{
	int to,next;
}edge[100010];
int head[5010],tail;
void add(int from,int to)
{
	edge[tail].to=to;
	edge[tail].next=head[from];
	head[from]=tail++;
}
void dfs(int from)
{
	dfn[from]=low[from]=++step;
	sk.push(from);
	insk[from]=1;
	for(int i=head[from];i!=-1;i=edge[i].next)
	{
		int to=edge[i].to;
		if(dfn[to]==0)
		{
			dfs(to);
			low[from]=min(low[from],low[to]);
		}
		else if(insk[to])
			low[from]=min(low[from],dfn[to]);
	}
	if(dfn[from]==low[from])
	{
		int v;
		do
		{
			v=sk.top();
			sk.pop();
			insk[v]=0;
			belong[v]=tol;
		}while(v!=from);
		tol++;
	}
}
int cost[5010],in[5010],mn[5010];
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			int v,c;
			scanf("%d%d",&v,&c);
			cost[v]=c;
		}
		tail=0;
		memset(head,-1,sizeof(head));
		while(m--)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			add(u,v);
		}
		tol=step=0;
		memset(dfn,0,sizeof(dfn));
		for(int i=1;i<=n;i++)
			if(dfn[i]==0)
				dfs(i);
		memset(mn,127,sizeof(mn));
		for(int i=1;i<=n;i++)
			mn[belong[i]]=min(mn[belong[i]],cost[i]);
		memset(in,0,sizeof(in));
		for(int i=1;i<=n;i++)
			for(int j=head[i];j!=-1;j=edge[j].next)
				if(belong[i]!=belong[edge[j].to])
					in[belong[edge[j].to]]++;
		int ans=0;
		for(int i=0;i<tol;i++)
			if(in[i]==0)
				ans+=mn[i];
		printf("%d\n",ans);
	}
}
时间: 2024-11-06 09:57:29

scoj2832: Mars city fzoj1462的相关文章

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

待刷题目分类

各大OJ题目归类 Posted on 2012 年 8 月 8 日 by admin ---------–最优化问题------------- --------动态规划 SOJ1162 I-Keyboard SOJ2096 Maximum Submatrix SOJ2111 littleken bg SOJ2505 The County Fair SOJ2818 QQ音速 SOJ2469 Exploring Pyramids SOJ1833 Base Numbers SOJ2009 Zeros

【BZOJ】1382: [Baltic2001]Mars Maps (线段树+扫描线)

1382: [Baltic2001]Mars Maps Time Limit: 5 Sec  Memory Limit: 64 MB Description 给出N个矩形,N<=10000.其坐标不超过10^9.求其面积并 Input 先给出一个数字N,代表有N个矩形. 接下来N行,每行四个数,代表矩形的坐标. Output 输出面积并 Sample Input 2 10 10 20 20 15 15 25 30 Sample Output 225 本以为是傻逼题,没想到不容易啊- 线段树+扫描

City Upgrades

City Upgrades Time limit: 1000 msMemory limit: 128 MB There are N cities placed in a line. For each city ii you know its coodinate x??. You can upgrade exactly K of these cities. Your goal is to choose what cities to upgrade in a way the minimizes th

hdu1800 Flying to the Mars(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14340    Accepted Submission(s): 4572 Problem Description In the year 8888

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space

在Eclipse mars 4.5.2 中安装spring 插件 spring tool suite

最近在学习spring,用到的IDE 有eclipse,也有用到 IDEA. 目前对spring还不是很了解,跟着网上的视频来,先学会了spring,然后再选IDE. 题归正转,下面说说怎么在Eclipse mars 4.5.2 中安装spring 插件 spring tool suite. 打开eclipse,然后在菜单栏中点击Help,选中Eclipse MarketPlace: 然后搜索STS(也就是spring tool suite的缩写),回车: 点击Install即可 由于是在线安装

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,

UIPicker city.plist(实例)

前言: UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.UIPickerView 直接继承了 UIView ,没有继承 UIControl ,因此,它不能像 UIControl 那样绑定事件处理方法, UIPickerView 的事件处理由其委托对象完成. 正文: UIPickerView 控件常用的属性和方法如下:   numberOfComponents: 获取UIPickerView指定列中