POJ 3160 Father Christmas flymouse

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 <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 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]] < d[u] + 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 < n; ++i)
 90             scanf("%d",hv+i);
 91         for(int i = 0; i < m; ++i){
 92             scanf("%d %d",&u,&v);
 93             add(u,v);
 94         }
 95         for(int i = 0; i < n; ++i)
 96             if(!dfn[i]) tarjan(i);
 97         for(int i = 0; i < n; ++i)
 98             if(hv[i] > 0) thv[belong[i]] += hv[i];
 99         for(int i = 0; i < n; ++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 }

时间: 2024-10-10 16:37:33

POJ 3160 Father Christmas flymouse的相关文章

poj 3160 Father Christmas flymouse 强连通+dp

首先我们可以确定的是,对于val值小于0的节点都变成0.   假设一个集合内2个房间都能任意到达,那么我就可以吧集合内的所有点的价值都取到,并且可以达到任一点.实际上集合内的每个点是相同的,这样的集合就是一个强连通分量. 那么我们就可以用tarjin算法进行强连通缩点, 最后形成一个dag的图.在dag的图上面进行dp.可以先用拓扑排序后dp.或者建反响边记忆化搜索 . VIEW  CDDE //#pragma comment(linker, "/STACK:102400000,10240000

poj 3160 Father Christmas flymouse【强连通 DAG spfa 】

和上一道题一样,可以用DAG上的动态规划来做,也可以建立一个源点,用spfa来做 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<stack> 6 #include<vector> 7 using namespace std; 8 9 const int maxn = 5005; 10 int n,

POJ——T3160 Father Christmas flymouse

Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3496   Accepted: 1191 缩点,然后每个新点跑一边SPFA 思路不难 ,注意细节~ 1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 6 using namespace std; 7 8 const

【连通图|强连通分量+dfs】POJ-3160 Father Christmas flymouse

Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description 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 contr

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA)

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n,树根为编号1,选择一些边,使得所有节点构成一棵树,选择边的代价是(子孙的点的重量)×(这条边的价值).求代价最小多少. 分析: 单看每个点被计算过的代价,很明显就是从根到节点的边的价值.所以这是个简单的单源最短路问题. 不过坑点还是很多的. 点的数量高达5w个,用矩阵存不行,只能用边存. 还有路径和结

POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n.树根为编号1,选择一些边.使得全部节点构成一棵树.选择边的代价是(子孙的点的重量)×(这条边的价值). 求代价最小多少. 分析: 单看每一个点被计算过的代价,非常明显就是从根到节点的边的价值.所以这是个简单的单源最短路问题. 只是坑点还是非常多的. 点的数量高达5w个,用矩阵存不行.仅仅能用边存. 还

poj 3031 Big Christmas Tree(水spfa)

http://poj.org/problem?id=3013 题意: Because of a technical difficulty, price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge).这句话一直没看懂.后面还以为是最小生成树. 正确题意是:给一个无向图,计算每个点到1节点的最短路径(dis[i]) * 每个节点的weights之和. 注意:边权值等要用__

poj 3013 Big Christmas Tree (dij+优先队列优化 求最短路)

模板 题意:给你一个图,1总是为根,每个边有单位价值,每个点有权重. 每条边的价值 = sum(后继节点权重)*边的单位价值. 求树的最小价值,即构成一棵树的n-1条边的最小价值. 算法: 1.因为每个边的价值都要乘以后来访问的节点的权重,而走到后来访问的点必经过这条边. 实际上总价值就是  到每个点的最短路径*这个点的权重. 2.但是这个题 数据量真的太大了,50000个点,50000条边. 写普通的dij算法tle. 必须加优先队列优化- - 据说spfa也能过,但是spfa算法不稳定- -

POJ 3013 Big Christmas Tree【最短路变形,DIjkstra堆优化+spfa算法】

Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23064   Accepted: 4987 Description Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of t