【拓扑】【宽搜】CSU 1084 有向无环图 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接:
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804
题目大意:
一个有向无环图(DAG),有N个点M条有向边(N,M<=105),每个点有两个值ai,bi(ai,bi<=109),count(i,j)表示从i走到j的方案数。
求
mod 109+7的值。
题目思路:
【拓扑】【宽搜】
首先将式子拆开,每个点I走到点J的d[j]一次就加上一次ai,这样一个点被i走到的几次就加上几次ai,相当于count(i,j)*ai,最终只要求Σ(bj*d[j])即可。
将这张有向无环图按照拓扑序加入队列,这样保证做到第i个点时在它之后不会再被更新答案(也就是说后面不会影响前面的值,这样才能通过前面的值推出后面的)
每个点I都把点I的后继节点的d加上ai,最终按照上面那样求Σ(bj*d[j])即可。
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 100004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
LL aans;
struct xxx
{
int next,to;
}a[N];
int last[N],q[N],in[N],aa[N],bb[N];
LL d[N];
void add(int x,int y)
{
a[++lll].to=y;
a[lll].next=last[x];
last[x]=lll;
}
void tuopu()
{
int i,l,r=;
for(i=;i<=n;i++)if(!in[i])q[++r]=i;
for(l=;l<=r && r!=n;l++)
for(i=last[q[l]];i;i=a[i].next)
if(--in[a[i].to]==)
q[++r]=a[i].to;
for(l=;l<=n;l++)
for(i=last[q[l]];i;i=a[i].next)
d[a[i].to]=(d[a[i].to]+d[q[l]]+aa[q[l]])%mod;
for(i=;i<=n;i++)
aans=(aans+1LL*d[i]*bb[i])%mod;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
while(~scanf("%d",&n))
{
lll=aans=;mem(last,);mem(in,);mem(d,);
scanf("%d",&m);
for(i=;i<=n;i++)scanf("%d%d",aa+i,bb+i);
for(i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
add(x,y);in[y]++;
}
tuopu();
printf("%lld\n",aans);
}
return ;
}
/*
// //
*/
【拓扑】【宽搜】CSU 1084 有向无环图 (2016湖南省第十二届大学生计算机程序设计竞赛)的更多相关文章
- 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...
- 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807 题目大意: 给你一个长度为N(N<=105)的数列,数列中的0可以被其他数 ...
- 【最短路】【数学】CSU 1806 Toll (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1806 题目大意: N个点M条有向边,给一个时间T(2≤n≤10,1≤m≤n(n-1), ...
- 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 题目大意: 一棵树,N(2<=N<=105)个节点,每个节点有一种颜 ...
- 【数学】CSU 1810 Reverse (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810 题目大意: 一个长度为N的十进制数,R(i,j)表示将第i位到第j位翻转过来后的 ...
- 【贪心】CSU 1809 Parenthesis (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 题目大意: 给一个长度为N(N<=105)的合法括号序列.Q(Q<= ...
- 【模拟】【数学】CSU 1803 2016 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1803 题目大意: 给定n,m(n,m<=109)1<=i<=n,1& ...
- 湖南省第十二届大学生计算机程序设计竞赛 B 有向无环图 拓扑DP
1804: 有向无环图 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 187 Solved: 80[Submit][Status][Web Board ...
- csu 1503: 点弧之间的距离-湖南省第十届大学生计算机程序设计大赛
这是--比量p并用交点连接中心不上弧.在于:它至p距离.是不是p与端点之间的最短距离 #include<iostream> #include<map> #include< ...
随机推荐
- JavaScript 使用
HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 <head> 部分 ...
- widows sever2003 PHP环境搭建
此文仅为文字笔记,非原创,摘阅自互联网 1.安装IIS6.0及Framework 2.0 2.安装fastcgi http://www.iis.net/download/FastCGI 安装fas ...
- 关于SqlServer修改数据库常用信息的方法
--系统表里存放各个数据库属性信息的表之一SELECT name AS [Logical Name], physical_name AS [DB File Path],type_desc AS [Fi ...
- Getting Started with Testing ——开始单元测试
Android tests are based on JUnit, and you can run them either as local unit tests on the JVM or as i ...
- Android-自定义多TAB悬浮控件实现蘑菇街首页效果
因为项目的一些需求需要用到此种展现方式. 找了市面上大部分有类似功能的应用. 基本思路嵌套ScrollView 转换事件分发给listview 实现. 但是此种方案有个缺点. 在ScrollVie ...
- ASP。net中如何在一个按钮click事件中调用另一个按钮的click事件
方法一: 直接指定 事件<asp:Button ID="btn1" runat="server" Text="按钮1" onclick ...
- 数据库导出excel表数据
- 执行之前 (错误) 消息 错误 0xc0202009: 数据流任务 1: SSIS 错误代码 DTS_E_OLEDBERROR.出现 OLE DB 错误.错误代码: 0x80040E37. (S ...
- C# RSA加密/解密
RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力的公钥加密算法, ...
- 启动scala的方法
1.从官网 http://www.scala-lang.org/download/ 下载scala二进制通用版本以后,在终端命令行添加下载解压包的bin目录到环境变量: export PATH=/Us ...
- java中的异常结构
1.基类为Throwable. 2.Error和Exception分别继承Throwable. 3.Error类异常描述了Java运行系统中的内部错误以及资源耗尽的情形.应用程序不应该抛出这种类型的对 ...