日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 3249】Test for Job(DAG线性求带负权的最长路,dp)

發布時間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 3249】Test for Job(DAG线性求带负权的最长路,dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job, since there are swelling numbers of the unemployed. So some companies often use hard tests for their recruitment.

The test is like this: starting from a source-city, you may pass through some directed roads to reach another city. Each time you reach a city, you can earn some profit or pay some fee, Let this process continue until you reach a target-city. The boss will compute the expense you spent for your trip and the profit you have just obtained. Finally, he will decide whether you can be hired.

In order to get the job, Mr.Dog managed to obtain the knowledge of the net profit?Viof all cities he may reach (a negative?Vi?indicates that money is spent rather than gained) and the connection between cities. A city with no roads leading to it is a source-city and a city with no roads leading to other cities is a target-city. The mission of Mr.Dog is to start from a source-city and choose a route leading to a target-city through which he can get the maximum profit.

Input

The input file includes several test cases.?
The first line of each test case contains 2 integers?n?and?m(1 ≤?n?≤ 100000, 0 ≤m?≤ 1000000) indicating the number of cities and roads.?
The next?n?lines each contain a single integer. The?ith line describes the net profit of the city?i,?Vi?(0 ≤ |?Vi| ≤ 20000)?
The next m lines each contain two integers?x,?y?indicating that there is a road leads from city?x?to city?y. It is guaranteed that each road appears exactly once, and there is no way to return to a previous city.?

Output

The output file contains one line for each test cases, in which contains an integer indicating the maximum profit Dog is able to obtain (or the minimum expenditure to spend)

Sample Input

6 5 1 2 2 3 3 4 1 2 1 3 2 4 3 4 5 6

Sample Output

7

Hint

解題報告:

? 因為有負權值所以初值要注意初始值要是-INF,并且只能用vis數組來標記是否走過,不能用dp[v]=-1來判斷是否走過,因為這里的dp有負權值所以不能把-1當成非法狀態。

? 這題也可以拓撲排序,每次彈出入度為0的頂點來更新其他節點,不斷維護e[i].v的最大值(因為e[i].u這個值是完成值也就是保證了是最優解,所以只能用我為人人法去更新),這樣也可以做到On的復雜度。

其實兩種方法一個是遞推dp(按照拓撲序當dp的階段)一個是記憶化搜索,本質是一樣的。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e6 + 5; const int INF = 0x3f3f3f3f; struct Edge {int to,ne; } e[MAX]; int head[MAX],in[MAX],out[MAX],tot,val[MAX],n,m; void add(int u,int v) {e[++tot].to = v;;e[tot].ne = head[u];head[u] = tot; } int dp[MAX],vis[MAX]; int dfs(int cur,int rt) {if(vis[cur] == 1) return dp[cur];vis[cur] = 1;int res = -INF;//int res = val[cur];這樣寫的話應該就錯了不信一會你自己試試 for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].to;res = max(res,dfs(v,cur));}if(head[cur] == -1) return dp[cur] = val[cur];else return dp[cur] = res + val[cur]; } int main() {while(~scanf("%d%d",&n,&m)) {tot=0;for(int i = 1; i<=n; i++) head[i] = -1,in[i]=out[i]=vis[i]=0,dp[i] = 0;for(int i = 1; i<=n; i++) scanf("%d",val+i);for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v),add(u,v);in[v]++;out[u]++;}int ans = -INF;//如果用0來賦初值的話呢?會有什么后果》 for(int i = 1; i<=n; i++) {if(in[i] == 0) {ans = max(ans,dfs(i,-1));}}printf("%d\n",ans);} return 0 ; }

?

總結

以上是生活随笔為你收集整理的【POJ - 3249】Test for Job(DAG线性求带负权的最长路,dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产成人精品无码免费看在线 | 国产精品夜夜爽张柏芝 | 片黄在线观看 | 午夜中出| 国产美女菊爆在线播放APP | 国产在线观看成人 | 免费在线黄色网址 | 精品视频一区二区三区四区五区 | 欧美一区二区三区视频 | 精品国产综合 | 国产ts人妖调教重口男 | 国产天堂视频 | 少妇的性事hd | 97xxx| 最新av观看| 骚虎视频在线观看 | 成人看片黄a免费看视频 | 韩日精品中文字幕 | 欧美私人影院 | 日韩a在线播放 | 国产精品一区二区欧美 | 久久伊人免费 | 国产精品久久国产精麻豆96堂 | 亚洲一区你懂的 | 五月婷婷久久久 | 污污内射久久一区二区欧美日韩 | 成人免费高清视频 | 借种(出轨高h) | 国产美女永久免费 | 91插插插影库永久免费 | 色屁屁www影院免费观看入口 | 久草福利资源站 | 色图色小说 | 中文字幕国产综合 | 三级艳丽杨钰莹三级 | 国产成人小视频 | 久久精品亚洲一区 | 玖玖色资源 | 免费网站看av | 毛片88 | 女人高潮潮呻吟喷水 | 两口子交换真实刺激高潮 | 国产精品老牛影视 | 国产视频一区二区在线播放 | 日韩大片av | 国产喷白浆一区二区三区 | 女优一区二区三区 | 最近中文字幕在线免费观看 | 熟女少妇一区二区三区 | 亚洲综合在线成人 | 丝袜调教91porn | 在线免费看av网站 | 小辣椒福利视频导航 | 亚洲综合视频在线观看 | 久热免费视频 | 国产精品久久久久国产a级 国产一区二区在线播放 | 国产剧情演绎av | 舒淇裸体午夜理伦 | 国产中文字幕视频 | 国产tv在线观看 | 免费av大片 | 四季av一区二区三区免费观看 | 中文字幕三级 | 日韩第一色| 男裸体无遮挡网站 | 国产三级视频网站 | 久久天天躁狠狠躁夜夜av | 国产又大又粗又硬 | www.超碰在线| 欧美大尺度做爰啪啪床戏明星 | 日韩三级黄色片 | 天天插天天狠天天透 | 少妇丰满尤物大尺度写真 | 婷婷在线一区 | 看黄色一级片 | 久久e热| 欧美日本道 | 97视频免费在线观看 | 国产毛片精品国产一区二区三区 | 女人私密又肥又大 | 欧美性一区二区 | 四虎影院黄色 | 亚洲欧美国产一区二区三区 | 成人毛片观看 | 欧美老熟妇一区二区三区 | 成人黄色一级视频 | a级欧美| 美女扒开让男人桶爽 | 亚洲欧美日韩国产一区 | 超碰在线免费公开 | 天堂精品久久 | 污污免费观看 | 日韩一及片 | 狠狠干超碰 | 国产精品午夜无码专区 | 国产精品永久免费视频 | 国产大学生自拍视频 | 超碰av在线免费观看 | 麻豆精品在线观看 |