ZOJ 3080 ChiBi(spfa)
Time Limit: 5 Seconds Memory Limit: 32768 KB
watashi's mm is so pretty as well as smart. Recently, she has watched the movie Chibi. So she knows more about the War of ChiBi. In the war, Cao Cao had 800,000 soldiers, much more than his opponents'. But he was defeated. One of the mistakes he made was that he connected some of his boats together, and these boats were burned by the clever opponents.
Then an interesting problem occurs to watashi's mm. She wants to use this problem to check whether watashi is as smart as her. However, watashi has no idea about the problem. So he turns to you for help.
You know whether two boats are directly connected and the distance between them. And Fire's speed to spread between boats is 1m/s. You also know the time your soldiers need to travel from your camp to each boat. Because burning Cao Cao's boat is a very dangerous job, you must choose the least number of soldiers, and each one can only burn one boat. How much time do you need to burn all the Cao Cao's boats?
Input
The input contains several test cases. Each test case begins with a line contains only one integer 0 <= N <= 1000, which indicates the number of boats. The next N lines, each line contains N integers in range [0, 10000], the jth number in the ith line is the distance in metre between the ith boat and the jth boat, if the number is -1, then these two boats are not directly connected (d(i, j) == d(j, i) && d(i, i) == 0). Then N intergers in range [0, 10000], the ith number is the time in second your soldiers need to travel from the camp to the ith boat. What's more Cao Cao is not that stupid, so he won't connect more than 100 boats together.
Output
The shortest time you need to burn all the Cao Cao's boats counting from the soldiers leave the camp in a single line.
Sample input
4
0 1 2 -1
1 0 4 -1
2 4 0 -1
-1 -1 -1 0
1 2 4 8
Sample Output
8
题意:
矩阵形式给出曹老板战船的联通情况(-1表示不联通), 权值表示距离,现在要你派若干人去放火,已知火势蔓延速度为1m/s,兵营到每艘船的时间已知,要求在派出最少人的情况下求出战船全部燃烧的最短时间。
分析:
首先这是个无向非联通图,派出的人最少也就是说每个联通块派一个人;然后我们需要枚举每一艘船作为起点,用SPFA(起点船到所有船的最短路径)选择最大的值表明最大的最短路径肯定经过所有点,所有联通块最大值中的最小值就是该联通块全部点燃的最短时间,然后取这些联通块最短时间的最大值就是所求全部燃烧的最短时间。
#include<queue>
#include<stdio.h>
#include<limits.h>
#include<string.h>
#include<iostream>
using namespace std;
#define MAX 1100
#define INF 10001
#define min(x,y) (x)<(y)? (x):(y)
#define max(x,y) (x)>(y)? (x):(y)
int map[MAX][MAX],startDis[MAX],n;
int grap[MAX][MAX],father[MAX],Count[MAX],hCount;
int visit[MAX];
void pre()
{
int i,j;
for(i=;i<n;i++){
for(j=;j<n;j++){
scanf("%d",&map[i][j]);
if(map[i][j]==-) map[i][j]=INF;
}
}
for(i=;i<n;i++) scanf("%d",&startDis[i]);
}
void dfs(int block[MAX],int &Count,int v)
{
int i;
block[Count++]=v;
for(i=;i<n;i++){
if(i!=v&&map[v][i]<INF&&visit[i]==){
visit[i]=;father[i]=v;
dfs(block,Count,i);
}
}
}
void init()
{
int i;
memset(Count,,sizeof(Count));
for(i=;i<n;i++) father[i]=i;
hCount=;
for(i=;i<n;i++){
if(father[i]==i){
memset(visit,,sizeof(visit));
visit[i]=;
dfs(grap[hCount],Count[hCount],i);
hCount++;
}
}
} int spfa(int v,int Count,int num[MAX])
{
int i,j;
int dis[MAX],times[MAX],inqueue[MAX];
queue<int> q;
for(i=;i<Count;i++) dis[i]=INT_MAX/;
memset(times,,sizeof(times));
memset(inqueue,,sizeof(inqueue));
times[v]=,inqueue[v]=,q.push(v);
dis[v]=;
while(!q.empty()){
int x=q.front();
q.pop(),inqueue[x]=;
for(i=;i<Count;i++){//枚举联通块中所有的点
if(num[v]!=num[i]&&map[num[x]][num[i]]<INF&&dis[i]>dis[x]+map[num[x]][num[i]]){
dis[i]=dis[x]+map[num[x]][num[i]];
if(inqueue[i]==){
q.push(i);inqueue[i]=;times[i]++;
if(times[i]>=Count) return -;
}
}
}
}
int mm=;
for(int i=;i<Count;i++)
mm=max(dis[i],mm);
return mm;
}
void solve()
{
int i,j,ans=,mi;
for(i=;i<hCount;i++){//每个联通块
mi=INT_MAX;
for(j=;j<Count[i];j++){//联通块中的所有点 联通块的索引
mi=min(mi,startDis[grap[i][j]]+spfa(j,Count[i],grap[i]));
}
ans=max(ans,mi);
}
printf("%d\n",ans);
}
int main(void)
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
pre();//接收数据
init();//获得联通块数组
solve();//遍历联通块中所有点
}
return ;
}
ZOJ 3080 ChiBi(spfa)的更多相关文章
- Haunted Graveyard ZOJ - 3391(SPFA)
从点(n,1)到点(1,m)的最短路径,可以转换地图成从(1,1)到(n,m)的最短路,因为有负权回路,所以要用spfa来判负环, 注意一下如果负环把终点包围在内的话, 如果用负环的话会输出无穷,但是 ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA
ZOJ Problem Set - 3946 Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the ...
- ZOJ 3362 Beer Problem(SPFA费用流应用)
Beer Problem Time Limit: 2 Seconds Memory Limit: 32768 KB Everyone knows that World Finals of A ...
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...
- ZOJ 3794 Greedy Driver spfa
题意: 给定n个点,m条有向边,邮箱容量. 起点在1,终点在n,開始邮箱满油. 以下m行表示起点终点和这条边的耗油量(就是长度) 再以下给出一个数字m表示有P个加油站,能够免费加满油. 以下一行P个数 ...
- ZOJ 2770 Burn the Linked Camp(spfa&&bellman)
//差分约束 >=求最长路径 <=求最短路径 结果都一样//spfa#include<stdio.h> #include<string.h> #include< ...
- (spfa) Highway Project (zoj 3946 )
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 Highway Project Time Limit: 2 Seco ...
- ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]
人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...
随机推荐
- ViewPager + HorizontalScrollView 实现可滚动的标签栏
这是一个可滑动的标签栏的自定义控件,参考此文章http://blog.csdn.net/fx_sky/article/details/8990573,我将主要的功能整合成一个类,配上2个特定的布局即可 ...
- Numpy之ndarray与matrix
1. ndarray对象 ndarray是numpy中的一个N维数组对象,可以进行矢量算术运算,它是一个通用的同构数据多维容器,即其中的所有元素必须是相同类型的. 可以使用array函数创建数组,每个 ...
- JavaScript常用内置对象(window、document、form对象)
由于刚开始学习B/S编程,下面对各种脚本语言有一个宏观的简单认识. 脚本语言(JavaScript,Vbscript,JScript等)介于HTML和C,C++,Java,C#等编程语言之间.它的优势 ...
- MySQL数据处理函数
数据处理函数 有时从数据库表中获取到的数据须要进行一些处理. 如将小写字母替换为对应的大写字母.这个处理过程能够在客户机上进行.也能够在数据库上进行. 数据库上进行会更高效.数据库中有对应的数据处理函 ...
- Swift 闭包表达式
闭包是功能性自包含模块,可以在代码中被传递和使用. Swift 中的闭包与 C 和 Objective-C 中的 blocks 以及其他一些编程语言中的 lambdas 比较相似. 闭包的形式主要有三 ...
- SQL学习之空值(Null)检索
在创建表表,我们可以指定其中的列包不包含值,在一列不包含值时,我们可以称其包含空值null. 确定值是否为null,不能简单的检查是否=null.select语句有一个特殊的where子句,可用来检查 ...
- ViewPager引导
1.首先要导入android-support.v4.jar第三方工具包 2.activity_guid.xml <RelativeLayout xmlns:android="http: ...
- Scala学习之for 循环和 yield 的例子
for循环中的 yield 会把当前的元素记下来,保存在集合中,循环结束后将返回该集合.Scala中for循环是有返回值的.如果被循环的是Map,返回的就是Map,被循环的是List,返回的就是Lis ...
- ndk 编译 boost 库,支持serialization
Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容.在C+ ...
- bzoj 1066 : [SCOI2007]蜥蜴 网络流
题目链接 给一个n*m的图, 里面每一个点代表一个石柱, 石柱有一个高度. 初始时有些石柱上面有蜥蜴, 蜥蜴可以跳到距离他曼哈顿距离小于等于d的任意一个石柱上,跳完后, 他原来所在的石柱高度会减一, ...