POJ-1926 Pollution
Pollution
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4049 Accepted: 1076
Description
The managers of a chemical plant, which is notorious for its high pollution, plan to adopt a newly developed device in order to reduce the amount of contaminants emitted. However, engineers in the plant are against this plan, as they doubt the usefulness of the device. As engineers only believe in experimental results, managers decide to hire programmers to make a numerical experiment to convince the engineers.
The new pollution-reducing device consists of several tanks with pipes connecting the tanks. You may assume there is at most one pipe between two tanks. Two tanks are called adjacent if a pipe connects them. When operating, the contaminant circulates in the device among these tanks.
As shown in the Figure-1, the contaminant in one tank in time t, will equally distribute into all adjacent tanks in the time t+1. In other words, if we use Xit to denote the amount of contaminant in tank i at time t, we can use the following formula:
where Iij=1 if tank i and tank j are adjacent, otherwise Iij=0, and where dj is the number of tanks adjacent to tank j. If no tank is adjacent to tank i, we have Xit+1=Xit.
The managers, as well as the engineers, want to know that given the initial amount of contaminant in each tank, how the contaminant will be distributed in all the tanks after a long period of time in circulation. Namely, given Xi0 for all i, what are Xit when the difference between Xit and Xit+1 is so small that it can be ignored. You may assume that this condition will ALWAYS be attained from an initial case in this problem.
Input
The first line of the input contains one integer T (1 <= T <= 10), the number of test cases. T cases then follow. For each test case, the first line consists of two integers: N and M where(1 <= N <= 100, 0 <= M <= N*(N-1)/2), is the number of tanks and pipes. The following N lines give the initial amount of contaminant for each tank, which are nonnegative real numbers and no larger than 100. Then the next M lines give the tanks that each pipe connects, as “A B” (1 <= A, B <= N, A != B) denotes there is a pipe between tank A and tank B.
Output
For each test case, output the final amount of contaminant Xit+1 (one per line), followed by a blank line. The number should be rounded to three digits after the decimal point.
Sample Input
2
3 3
1
0
0
1 2
2 3
3 1
4 4
1
0
0
1
1 2
2 3
3 1
3 4
Sample Output
0.333
0.333
0.333
0.500
0.500
0.750
0.250
这道题目直接模拟也能过,但应该不是出题者的本意。一般的思路就是一个连通块里面的总的污染气体的量按入度分配,这里入度和出度一样的
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int a[105][105];
int degree[105];
int n,m;
double c[105];
int vis[105];
void floyed()
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][k]&&a[k][j]&&i!=j)
a[i][j]=1;
}
}
}
}
int main()
{
int t;
int x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%lf",&c[i]);
memset(a,0,sizeof(a));
memset(degree,0,sizeof(degree));
memset(vis,0,sizeof(vis));
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(!a[x][y])
{
a[x][y]=a[y][x]=1;
degree[x]++;
degree[y]++;
}
}
floyed();
for(int i=1;i<=n;i++)
{
if(vis[i])
continue;
vis[i]=1;
double sum=0;
double num=0;
sum+=c[i];
num+=degree[i];
for(int j=1;j<=n;j++)
{
if(!vis[j]&&a[i][j])
{
sum+=c[j];
num+=degree[j];
}
}
if(num==0)
{
printf("%.3f\n",c[i]);
continue;
}
double p=sum/(double)num;
printf("%.3f\n",p*degree[i]);
for(int j=1;j<=n;j++)
{
if(!vis[j]&&a[i][j])
{
printf("%.3f\n",p*degree[j]);
vis[j]=1;
}
}
}
printf("\n");
}
return 0;
}
POJ-1926 Pollution的更多相关文章
- 专题:DP杂题1
A POJ 1018 Communication System B POJ 1050 To the Max C POJ 1083 Moving Tables D POJ 1125 Stockbroke ...
- poj动态规划列表
[1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...
- POJ 动态规划题目列表
]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...
- [转] POJ DP问题
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- POJ动态规划题目列表
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- DP题目列表/弟屁专题
声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 10 ...
- poj 题目分类(1)
poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...
- POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)
本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...
- poj 动态规划题目列表及总结
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
随机推荐
- Go 语言机制之逃逸分析
https://blog.csdn.net/weixin_38975685/article/details/79788254 Go 语言机制之逃逸分析 https://blog.csdn.net/ ...
- task.factory.startnew()
1.委托: public delegate int Math(int param1,int param2);定义委托类型 Public int Add(int param1,int param2)// ...
- WPF布局管理之Canvas、InkCanvas (转)
一.Canvas 在WPF中子元素的绝对定位的布局控件 其子元素使用Width.Height定义元素的宽度和高度 使用Convas.Left(Convas.Right).Convas.Top(Conv ...
- Android开发-- 简单对话框
final Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.appicns_folder_sma ...
- 关于 Handler 与 opener
我们可以使用 urllib.request.Request() 构造请求对象,但是对于一些更高级的操作,比如 Cookies 处理.代理设置 .身份验证等等,Request() 是处理不了的这时就需要 ...
- Unity 蓝牙插件
1.新建一个Unity5.6.2f1工程,导入正版Bluetooth LE for iOS tvOS and Android.unitypackage2.用JD-GUI反编译工具查看unityandr ...
- [Windows] 解决 VLC Media Player 的 Crash Reporting 消息弹窗
运行环境:Windows 8.1 (64bits), VLC Media Player 2.1.3 异常描述:首次启动VLC播放影音文件时,一切正常.此后每次启动VLC都弹出"VLC Cra ...
- js for循环与for in循环的区别
for循环可一遍历数组,而for in循环可以遍历数组和对象 使用for in循环会将Array当成对象遍历,而Array的存取速度明显比Object要快.所以使用for循环遍历数组比for in循环 ...
- Hibernate系列之ID生成策略
一.概述 hibernate中使用两种方式实现主键生成策略,分别是XML生成id和注解方式(@GeneratedValue),下面逐一进行总结. 二.XML配置方法 这种方式是在XX.hbm.xml文 ...
- 【转载】6种.net分布式缓存解决方案
. 使用内置ASP.NET Cache (System.Web.Caching) : https://msdn.microsoft.com/en-us/library/system.web.cachi ...