热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

POJ3160FatherChristmasflymouse

FatherChristmasflymouseTimeLimit:1000msMemoryLimit:131072KBThisproblemwillbejudgedonPKU.Or

Father Christmas flymouse

Time Limit: 1000ms
Memory Limit: 131072KB
This problem will be judged on PKU. Original ID: 3160
64-bit integer IO format: %lld      Java class name: Main
 

After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such as cleaning out the computer lab for training as extension of his contribution to the team. When Christmas came, flymouse played Father Christmas to give gifts to the team members. The team members lived in distinct rooms in different buildings on the campus. To save vigor, flymouse decided to choose only one of those rooms as the place to start his journey and follow directed paths to visit one room after another and give out gifts en passant until he could reach no more unvisited rooms.

During the days on the team, flymouse left different impressions on his teammates at the time. Some of them, like LiZhiXu, with whom flymouse shared a lot of candies, would surely sing flymouse’s deeds of generosity, while the others, like snoopy, would never let flymouse off for his idleness. flymouse was able to use some kind of comfort index to quantitize whether better or worse he would feel after hearing the words from the gift recipients (positive for better and negative for worse). When arriving at a room, he chould choose to enter and give out a gift and hear the words from the recipient, or bypass the room in silence. He could arrive at a room more than once but never enter it a second time. He wanted to maximize the the sum of comfort indices accumulated along his journey.

Input

The input contains several test cases. Each test cases start with two integers N and M not exceeding 30 000 and 150 000 respectively on the first line, meaning that there were N team members living in N distinct rooms and M direct paths. On the next N lines there are N integers, one on each line, the i-th of which gives the comfort index of the words of the team member in the i-th room. Then follow M lines, each containing two integers i and j indicating a directed path from the i-th room to the j-th one. Process to end of file.

 

Output

For each test case, output one line with only the maximized sum of accumulated comfort indices.

 

Sample Input

2 2
14
21
0 1
1 0

Sample Output

35

Source

POJ Monthly--2006.12.31
 
解题:强连通缩点+最长路
,,
  1 #include 
  2 #include 
  3 #include 
  4 #include 
  5 #include 
  6 #include 
  7 #include 
  8 #include 
  9 #include 
 10 #include <string>
 11 #include <set>
 12 #include 
 13 #define LL long long
 14 #define pii pair
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 30010;
 18 struct arc{
 19     int to,next;
 20     arc(int x = 0,int y = -1){
 21         to = x;
 22         next = y;
 23     }
 24 };
 25 arc e[maxn*10];
 26 vector<int>g[maxn];
 27 int tot,head[maxn],d[maxn],dfn[maxn],low[maxn],belong[maxn];
 28 int hv[maxn],thv[maxn],scc,idx;
 29 int my[maxn],in[maxn],out[maxn],top,n,m;
 30 bool instack[maxn],inq[maxn];
 31 void add(int u,int v){
 32     e[tot] = arc(v,head[u]);
 33     head[u] = tot++;
 34 }
 35 void tarjan(int u){
 36     dfn[u] = low[u] = ++idx;
 37     my[top++] = u;
 38     instack[u] = true;
 39     for(int i = head[u]; ~i; i = e[i].next){
 40         if(!dfn[e[i].to]){
 41             tarjan(e[i].to);
 42             low[u] = min(low[u],low[e[i].to]);
 43         }else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
 44     }
 45     if(low[u] == dfn[u]){
 46         int v;
 47         scc++;
 48         do{
 49             v = my[--top];
 50             instack[v] = false;
 51             belong[v] = scc;
 52         }while(v != u);
 53     }
 54 }
 55 void init(){
 56     for(int i = 0; i <= n; ++i){
 57         head[i] = -1;
 58         dfn[i] = low[i] = 0;
 59         out[i] = belong[i] = 0;
 60         inq[i] = instack[i] = false;
 61         g[i].clear();
 62         hv[i] = thv[i] = 0;
 63         in[i] = d[i] = 0;
 64     }
 65     tot = idx = scc = 0;
 66 }
 67 void spfa(){
 68     queue<int>q;
 69     q.push(0);
 70     while(!q.empty()){
 71         int u = q.front();
 72         q.pop();
 73         inq[u] = false;
 74         for(int i = g[u].size()-1; i >= 0; --i){
 75             if(d[g[u][i]]  thv[g[u][i]]){
 76                 d[g[u][i]] = d[u] + thv[g[u][i]];
 77                 if(!inq[g[u][i]]){
 78                     inq[g[u][i]] = true;
 79                     q.push(g[u][i]);
 80                 }
 81             }
 82         }
 83     }
 84 }
 85 int main() {
 86     int u,v,w;
 87     while(~scanf("%d %d",&n,&m)){
 88         init();
 89         for(int i = 0; i i)
 90             scanf("%d",hv+i);
 91         for(int i = 0; i i){
 92             scanf("%d %d",&u,&v);
 93             add(u,v);
 94         }
 95         for(int i = 0; i i)
 96             if(!dfn[i]) tarjan(i);
 97         for(int i = 0; i i)
 98             if(hv[i] > 0) thv[belong[i]] += hv[i];
 99         for(int i = 0; i i){
100             for(int j = head[i]; ~j; j = e[j].next){
101                 if(belong[i] == belong[e[j].to]) continue;
102                 g[belong[i]].push_back(belong[e[j].to]);
103                 in[belong[e[j].to]]++;
104                 out[belong[i]]++;
105             }
106         }
107         for(int i = 1; i <= scc; ++i)
108             if(!in[i]) g[0].push_back(i);
109         int ans = 0;
110         spfa();
111         for(int i = 1; i <= scc; ++i)
112             if(!out[i]) ans = max(ans,d[i]);
113         printf("%d\n",ans);
114     }
115     return 0;
116 }
View Code

POJ 3160 Father Christmas flymouse


推荐阅读
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • PDF内容编辑的两种小方法,你知道怎么操作吗?
    本文介绍了两种PDF内容编辑的方法:迅捷PDF编辑器和Adobe Acrobat DC。使用迅捷PDF编辑器,用户可以通过选择需要更改的文字内容并设置字体形式、大小和颜色来编辑PDF文件。而使用Adobe Acrobat DC,则可以通过在软件中点击编辑来编辑PDF文件。PDF文件的编辑可以帮助办公人员进行文件内容的修改和定制。 ... [详细]
author-avatar
倩倩
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有