Hust1608 Dating With Girls hust BFS seeking the longest path

Source: Internet
Author: User

Dating With GirlsTime Limit: 1 Sec Memory Limit: 128 MBSubmissions: 146 Solved: 22 DescriptionIf 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 shoshould 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 wowould 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 helpInpu TFirst 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 Input3 31 21 32 3-3010Sample Output30HINTSourceThe 7th (2012) ACM Programming Contest of HUSTProblem Setter: Senlan Yao [cpp]/* Meaning: There are n points in a graph. The boy is, each girl has a wi value between 2 and n and asks the boy to reach the minimum wi value (wi guarantees that the minimum wi value is less than 0) the girl who does not pass through any wi <0 and obtains the most value ?, When a boy passes through a girl, he will get the value of | wi |. Train of Thought: the first point is a good point, and the end point is wi (wi <0, the longest path without passing through any wi <0 point) in the middle, and the obtained value of the wi <0 is the smallest; */# include <stdio. h> # include <string. h> # include <queue> # include <math. h> using namespace std; int vis [105], map [105] [105], val [105], mmax [105], n; void BFS () {int I; memset (vis, 0, sizeof (vis); for (I = 1; I <= n; I ++) mmax [I] =-1000; queue <int> que; mmax [1] = 0; que. push (1); vis [1] = 1; while (! Que. empty () {int temp = que. front (); que. pop (); for (I = 1; I <= n; I ++) {if (map [temp] [I]) /* if there is a way to go from temp to I, whether or not it is passed through another path first, see whether the existing path can make the point longer. if yes, update */{if (val [I]> 0) // if val [I]> 0 indicates that the value of point I is greater than 0, directly add {if (mmax [I] <mmax [temp] + val [I]) mmax [I] = mmax [temp] + val [I];} else // if val [I] <0 is the value corresponding to the I point <Add the opposite number to 0 {if (mmax [I] <mmax [temp]-val [I]) mmax [I] = mmax [temp]-val [I];} if (! Vis [I] & val [I]> 0) // if this point has already passed, there is no need to join again. In addition, according to the question, the last point must be smaller than 0, so you cannot join a point smaller than 0 {que. push (I); vis [I] = 1 ;}} vis [temp] = 0 ;}} int main () {int I, m, x, y, min, flag; while (scanf ("% d", & n, & m )! = EOF) {memset (map, 0, sizeof (map); while (m --) {scanf ("% d", & x, & y ); map [x] [y] = 1;} min = 10000; for (I = 2; I <= n; I ++) {scanf ("% d ", & val [I]); if (min> val [I]) {min = val [I]; flag = I ;}} BFS (); if (mmax [flag]> = 0) printf ("% d \ n", mmax [flag]); else printf ("What is a fucking day! \ N ") ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.