(点击此处查看原题)

题意

给出一个有n个结点,m条边的DAG图,每个点都有权值,每条路径(注意不是边)的权值为其经过的结点的权值之和,每条路径总是从入度为0的点开始,直至出度为0的点,问所有路径中权值最大者为多少,如下图,加粗的为权值最大者:

解题思路

这是在一个无起点、终点的图中的求最长路的问题,因此无法像一般的最长路问题那样求解。

首先,因为图中只存在点权,为了方便,我们一般将点权转化为边权:取每条边的权值为其终点的权值,将点权转化为边权。

然后,由于我们每条路径都是以入度为0的点开始,以出度为0的点结束,而且是求最大路,我第一想法就是AOV网中求事件的最晚完成时间,这两者是很相似的,不同的在于AOV网中有一个入度为0的源点和一个出度为0的汇点,而这个地方有多个入度为0的点和多个出度为0的点,不过实际的操作是完全一致的,都是利用拓扑排序的效果求最长路

为了方便理解,可以假设存在一个权值为0的点s向所有入度为0的点建边,然后把这个点当作起点,利用求拓扑序的时候,可以求出事件的最晚完成时间的效果,求其余各点到这个点的最长路,最后求出所有出度为0的点到s的最长路中的最大者,即为答案

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
#include<fstream>
#include<vector>
#include<stack>
#include <map>
#include <iomanip> #define bug cout << "**********" << endl
#define show(x, y) cout<<"["<<x<<","<<y<<"] "
#define LOCAL = 1;
using namespace std;
typedef long long ll;
const ll inf = 1e18+;
const ll mod = 1e9 + ;
const int Max = 1e6 + ;
const int Max2 = 3e2 + ; struct Edge
{
int to,next;
ll dis;
}edge[Max<<]; int n, m;
ll val[Max];
int head[Max],tot;
int topo[Max],id; //记录每个点的拓扑序
int d_in[Max],d_out[Max]; //记录每个点的入度和出度
ll dist[Max]; //距离源点的最大距离 int init()
{
memset(head,-,sizeof(head));
tot = ;
memset(topo,,sizeof(topo));
id = ;
memset(d_in,,sizeof(d_in));
memset(d_out,,sizeof(d_out));
} void add(int u,int v,ll dis)
{
edge[tot].to = v;
edge[tot].dis = dis;
edge[tot].next = head[u];
head[u] = tot++;
} void topoSort()
{
queue<int>q; //存储入度为0的点
for(int i = ;i <= n ;i ++)
{
if(d_in[i] == )
{
q.push(i);
dist[i] = val[i]; //初始化
}
}
while(!q.empty())
{
int u = q.front();q.pop();
topo[u] = ++id;
for(int i = head[u] ; i != - ; i = edge[i].next)
{
int v = edge[i].to;
ll dis = edge[i].dis;
if(!topo[v])
{
d_in[v] --;
dist[v] = max(dist[v],dist[u] + dis);
if(d_in[v] == )
q.push(v);
}
}
} } int main()
{
#ifdef LOCAL
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i = ;i <= n ;i ++)
scanf("%lld",val+i);
for(int i = , u, v ; i <= m ;i ++)
{
dist[i] = -inf;
scanf("%d%d",&u,&v);
d_out[u] ++;
d_in[v]++;
add(u,v,val[v]); //以终点的权值作为边的权值
}
topoSort();
ll max_dis = -inf;
for(int i = ;i <= n ;i ++)
{
if(d_out[i] == ) //只对出度为0的点进行判断
{
max_dis = max(max_dis,dist[i]);
}
}
printf("%lld\n",max_dis);
}
return ;
}

POJ - 3249 Test for Job (在DAG图利用拓扑排序中求最长路)的更多相关文章

  1. POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;&amp; SPFA求最长路 &amp;&amp; 经典】

    Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accep ...

  2. Vulnerable Kerbals CodeForces - 772C【拓展欧几里得建图+DAG上求最长路】

    根据拓展欧几里得对于同余方程 $ax+by=c$ ,有解的条件是 $(a,b)|c$. 那么对于构造的序列的数,前一个数 $a$  和后一个数 $b$ ,应该满足 $a*x=b(mod m)$ 即 $ ...

  3. 训练赛 Grouping(强连通分量缩点 + DAG求最长路)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=158#problem/F 大致题意:给出n个人和m种关系(ti,si),表示ti ...

  4. 【bzoj5017】[Snoi2017]炸弹 线段树优化建图+Tarjan+拓扑排序

    题目描述 在一条直线上有 N 个炸弹,每个炸弹的坐标是 Xi,爆炸半径是 Ri,当一个炸弹爆炸时,如果另一个炸弹所在位置 Xj 满足:  Xi−Ri≤Xj≤Xi+Ri,那么,该炸弹也会被引爆.  现在 ...

  5. poj 2060 Taxi Cab Scheme(DAG图的最小路径覆盖)

    题意: 出租车公司有M个订单. 订单格式:     hh:mm  a  b  c  d 含义:在hh:mm这个时刻客人将从(a,b)这个位置出发,他(她)要去(c,d)这个位置. 规定1:从(a,b) ...

  6. UVA 10029 Edit Step Ladders ——(DAG求最长路)

    题意:升序的给出一本若干个单词,每个单词都可删除一个字母,添加一个字母或者改变一个字母,如果任意一个操作以后能变成另外一个字典中的单词,那么就连一条有向边,求最长的长度. 分析:DAG的最长路和最短路 ...

  7. POJ 2762推断单个联通(支撑点甚至通缩+拓扑排序)

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14789 ...

  8. POJ 2472 106 miles to Chicago(Dijstra变形——史上最坑的最长路问题)

    题目链接 :http://poj.org/problem?id=2472 Description In the movie "Blues Brothers", the orphan ...

  9. Paint the Grid Again (隐藏建图+优先队列+拓扑排序)

    Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...

随机推荐

  1. typedef struct和指针

    在学习链表时遇到了typedef已经用typedef定义的指针,不是很懂,某浪里有位博主的博文写的很详细,我直接粘过来 假设我们定义一个结构体: typedef struct ANSWER_HEADE ...

  2. CodeForces 755D PolandBall and Polygon ——(xjbg)

    每次连线,起点和终点之间,每一个被点亮的点,这些点都能连出去两条线,因此可以增加的块数+2(1这个点除外,因为只有连出的点没有连进的点),计算起点和终点之间有几个点被点亮即可,然后1这个点特判一下.感 ...

  3. idea2018.3.2版本如何破解

    IntelliJ IDEA2018破解教程(2019.1.11更新)破解方法:下载破解补丁→修改配置文件→输入激活码→激活成功 由于JetBrains封杀,大部分激活服务器已经不能使用,使用下面的比较 ...

  4. Failed to execute aapt

    Failed to execute aapt 没错,看到这个表示你的资源出错了.不用想别的. 比如: Failed to execute aapt com.android.ide.common.pro ...

  5. requests和BeautifulSoup模块的使用

    用python写爬虫时,有两个很好用第三方模块requests库和beautifulsoup库,简单学习了下模块用法: 1,requests模块 Python标准库中提供了:urllib.urllib ...

  6. 火车购票问题(16年ccf)

    火车购票问题(16年ccf) 问题描述 请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配. 假设一节车厢有20排.每一排5个座位.为方便起见,我们用1到100来给所有的座位编号,第一 ...

  7. LC 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. monkey 查找闪退页面的方法

    使用了命令 adb shell monkey  --pct-touch 100 -v -p  com.iBer.iBerAppV2  5000 >/Users/kaibinliu/Desktop ...

  9. 用Python计算三角函数之acos()方法的使用

    用Python计算三角函数之acos()方法的使用 acos()方法返回x的反余弦值,以弧度表示. 语法 以下是acos()方法的语法:     acos(x) 注意:此函数是无法直接访问的,所以我们 ...

  10. MongoDB集群-主从复制(副本集)、failover

    1.概念 主从复制的目的:数据冗余.备份.读写分离 主从方式:一主一从(不推荐,只能实现复制,主节点挂掉且未重新启动的时候,无法提升从节点为master),一主一从一裁判,一主多从 复制方式:主节点记 ...