1. 程式人生 > >1608 求帶權有向圖上的最長路

1608 求帶權有向圖上的最長路

If you have a date with a pretty girl in a hurry, you can ignore what I will say next. 
  
Hellis is a little bad guy in Rural Small Technical College. And the most important fact is that he is especially fond of glaring at pretty girls! And as we all know, there are some girls he favors in the same school. So there comes the trouble. On one terrible day, it should rains. The girls all are being different places. They all phone him and ask the boy to deliver them umbrellas at the same time. However, the cute boy faces the embarrassing condition. You can have a simple Understanding that each girl has a special relationship with our sunny boy. If they know the boy sends the umbrella to anyone of them, the others may go mad and that is not the boy expects. If that happens, Of course you will never see that he is completing again. But the fact is some girls are so beautiful and he would like to give them help. The trouble is this guy wants to meet more beautiful girls before he arrives at anyone who phones him for help. It is just a disaster, he thinks. Eventually, he makes the choice. He will just send an umbrella to only one girl who is most important to him, and he can not be seen by other girls who phones him for help. If that happens, I can only say hehe
. He must pass these girls. In the process, he can send beautiful girls their umbrellas! In that case, he can create a chance to communicate with them and just win their favorable impression. It is such a good idea! 
  
There are n different places in our problem. There are m different undirectional edges. And there is no loop. The bad guy starts at 1 node, and other 2 to n node stand different girls. Each girl is standing at different nodes, too. The i-th girl has wi. When Hellis meets a girl, he can get |wi| point, and he wants to get max sum of point he gets.(wi<0 mean the i-th girl has phoned him for help 
InputFirst line, two integers, n ,m (1<n<100, 1<m<5000) 
2th
 to (m+1) th per line , ai, bi (0<ai<=n, 0<bi<=n) means one road from ai to bi. 
(m+2) th to (n+m) th per line, wi (0<| wi|<=100, 2<=i<=n)  
OutputIf the guy can meet the girl they chose, output line print a single integer ans — the max sum of point he gets. 
Else print “What is a fucking day!”  
Sample Input
3 3
1 2
1 3
2 3
-30
10
Sample Output
30


最長路的求法類似最短路,只不過每個點向拓展的時候都要先更新一遍dp[i],拓展完最後要把該點的vis值改回零

#include<bits/stdc++.h>
#define ll long long
#define inf 0x7fffffff
#define mod 1000000007
using namespace std;
vector<int> v[105];
int w[105];
int vis[105];
int dp[105];
int main(){
	int n,m,a,b;
	cin>>n>>m;
	for(int i=0;i<m;++i){
		cin>>a>>b;
		v[a].push_back(b);
	}
	int minn=inf,id=-1;
	for(int i=2;i<=n;++i){
		cin>>w[i];
		if(w[i]<0){
			if(w[i]<minn){
				minn=w[i];
				id=i;
			}
		}
	}
	if(id==-1){
		cout<<"What is a fucking day!"<<endl;
		return 0;
	}
	queue<int> q;
	q.push(1);
	vis[1]=1;
	while(!q.empty()){
		int fr=q.front();
		q.pop();
		for(int i=0;i<v[fr].size();++i){
			a=v[fr][i];
			if(w[a]>=0||a==id)dp[a]=max(dp[a],dp[fr]+abs(w[a]));
			if(vis[a]==1||w[a]<0) continue;
			vis[a]=1;
			q.push(a);
		}
		vis[fr]=0; //????
	}
	if(dp[id]==0) cout<<"What is a fucking day!"<<endl;
	else cout<<dp[id]<<endl; 
}  


相關推薦

1608

If you have a date with a pretty girl in a hurry, you can ignore what I will say next.    Hellis is a little bad guy in Rural Small Tech

實驗七 采用Dijkstra算法短路徑

圖解 初始 -s 由於 mic spa 初始化 mil dijkstra Dijkstra算法圖解 說明:初始化:S = { 0 }, U = { 1, 2, 3, 4, 5, 6 }, dist[ ] = { 0, 4, 6, 6, ∞, &in

資料結構——短路徑演算法Dijkstra演算法)

Dijkstra演算法是由荷蘭電腦科學家艾茲格·迪科斯徹發現的。演算法解決的是有向圖中最短路徑問題。 舉例來說,如果圖中的頂點表示城市,而邊上的權重表示著城市間開車行經的距離。 Dijkstra演算法可以用來找到兩個城市之間的最短路徑。 Dijkstra演算法的輸入包含了一個有權重的有向圖G,以及G中的一個

12.中任意兩點間的短路徑

其實它的程式碼理解起來真的挺難的我覺得!!! 昨天看了一下午感覺晦澀難懂,還是matlab好用,直接呼叫函式就可以了!!! 不過這裡還是得跟大家介紹一下: 1.問題的理解: 像這種帶權的有向圖,每一行都表示該行標號對應列標號的有向權值,本身到本身的數值為0,沒辦法

【ACM】單源短路徑(Dijkstra演算法)

最短路徑的第一類問題 求從單個源點到其餘各頂點的最短路徑。這是一種貪心策略,不可以存在負權邊。 演算法簡介 給定帶權有向圖G和源點v0,求從源點v0到G中其餘各頂點的最短路徑。迪傑斯特拉演算法是對

路徑及是否存在環路結構

問題描述: 有n個長為m+1的字串,如果某個字串的最後m個字元與某個字串的前m個字元匹配,則兩個字串可以連線。問這n個字串最多可以連成一個多長的字串,如果出現迴圈,則返回錯誤。 分析: 把每個字串看成一個圖的頂點,兩個字串可以連線就形成一條有向邊。相當於判斷一個有向圖是否存

通過按層遍歷的兩點間的短路徑

/** * * @param a * Person a * @param b * Person b * @return a與b之間的距離,返回 0 如果a=b,返

Tarjan無縮環(邊雙)/縮環(邊雙)/無點雙

邊雙與點雙 不嚴謹的定義, 邊雙=刪掉一條邊依然連通 點雙=刪掉一個點依然連通 無向圖Tarjan求邊雙 先說無向圖。無向圖就比有向圖簡單一些,因為只有返祖邊而沒有橫叉邊。 用棧來儲存已訪問的點。 如果已經訪問過了,就把它當返祖邊處理(low=min(low,

poj 1734 Floyd算小環

題意:旅遊公司要開發一條新的路線 , 要求這是一個總路程儘可能短的環 , 並且不能只含兩個城市 , 除開起點外 , 不能重複走之前走過的城市 , 輸出這條路線? Floyd演算法求最小環 程式碼: //用floyd演算法 , 求有向圖的最小環 #include #include #include #i

平衡的異步隨機投影算法

叠代 AMM amp ron 特征 取值 quad 現在 最優 Asynchronous Gossip-Based Random Projection Algorithm Over Networks 概述本篇論文討論的是在有向平衡的拓撲結構下的,帶約束的分布式次梯度投影算

大路徑

 阿里巴巴的一個程式設計測驗 輸入: 5 4 3 2 10 5 7 1 2 1 3 2 5 4 5 輸出:  3 13   #include <iostream> #include <algorithm> using n

uva 11183 小生成樹

AC程式碼 #include<bits/stdc++.h> using namespace std; #define N 1300 int pre[N]; int in[N]; int id[N]; int v[N]; struct edge{

短路徑演算法

最短路徑演算法屬於資料結構的圖的應用知識。先介紹基本的圖的概念。 圖由頂點集和邊集組成。(一張圖裡不就是有頂點和邊)。圖中邊帶有方向就是有向圖,否則就是無向圖。圖的儲存結構分為鄰接表和鄰接矩陣。(鄰接表主要採用順序儲存和鏈式儲存結合的方式)。採用連結表這種都是對於稀疏圖而言

【資料結構與演算法】 短路徑實現

Goal: Practice the algorithms of shortest pathproblem. Task:用一個有向圖表示給定的n個(要求至少10個)城市(或校園中的一些地點)及其之間的道路、距離情況,道路是有方向的。要求完成功能:根據使用者輸入的任意兩個城

小樹形圖【小生成樹】

  做了POJ的一道題,總是WA,不知道為什麼,後來去看了,才知道,原來有向圖的最小生成樹與無向圖不一樣,它得是從某個點出發能遍歷到其他所有點的才行,以此為條件,我們學習到了最小樹形圖。   與很多其他人的講解不一樣吧,我把我看了部落格後自己的思路分享出來,關於什麼是最小

小樹形圖--小生成樹 poj 3164 Command Network

【最小樹形圖】: 就是給有向帶權圖中指定一個特殊的點root,求一棵以root為根的有向生成樹T,並且T中所有邊的總權值最小。 最小樹形圖必須有一個根,而且選擇不同的點作為根,也是不一樣的結果。 最小樹形圖必須包含圖中的每一個節點,並且均可通過有向邊到達根節點root 最

[轉載]小生成樹,小樹形圖

轉載: 有固定根的最小樹形圖求法O(VE): 首先消除自環,顯然自環不在最小樹形圖中。然後判定是否存在最小樹形圖,以根為起點DFS一遍即可。 之後進行以下步驟。 設cost為最小樹形圖總權值。 0.置cost=0。 1.求最短弧集合Ao (一條弧就是

短路徑(Floyd演算法)

最近在研究最短路徑演算法,使用java實現。 原始資料是一共有6個點,他們之中任意2個點(i,j)之間的距離v(i,j)的數值如下面二位陣列中所示,整體演算法使用Java語言實現。 class Floyd{ public static void main(S

【BZOJ3659】Which Dreamed It【歐拉回計數】【matrix tree定理】【BEST定理】【高斯消元】

定理題... /* Think Thank Thunk */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typede

每日一省之————加權短路徑問題

1 加權有向圖中邊的資料結構 /** * 該類用於表示有向圖中的一條有向邊 * @author lhever 2017年3月2日 下午11:25:30 * @version v1.0 */ public class DirectedEdge {