程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hust1608 Dating With Girls hust校賽 BFS求最長路

hust1608 Dating With Girls hust校賽 BFS求最長路

編輯:C++入門知識

  Dating With Girls Time Limit: 1 Sec  Memory Limit: 128 MB Submissions: 146  Solved: 22 Description 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 Input First 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)  Output If 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 HINT Source The 7th(2012) ACM Programming Contest of HUST Problem Setter: Senlan Yao   [cpp]   /*  題意:  一個圖上有n個點,男孩在1號點,女孩在2至n個點,每個女孩有一個值wi,問男孩到達最小wi值(wi保證最小的小於0)  的那個女孩且中間不經過任何wi<0的女孩並獲得最多的值為多少?,男孩每經過一個女孩他將獲得|wi|的值;  思路:以1好點為起點,求一個終點為wi且(wi<0,中間不經過任何wi<0的點)的最長路,輸出wi<0中wi最小的那個所得的值;  */   #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])/*如果temp到i有路可走 那麼先不管是否通過其它路徑走過 先看通過現有路徑能不能使該點更長 如果可以那麼就更新*/               {                   if(val[i]>0)//如果val[i]>0 即i點對應的值大於0直接加                   {                       if(mmax[i]<mmax[temp]+val[i])                           mmax[i]=mmax[temp]+val[i];                   }                   else//如果val[i]<0 即i點對應的值<於0加其相反數                   {                       if(mmax[i]<mmax[temp]-val[i])                           mmax[i]=mmax[temp]-val[i];                     }                   if(!vis[i]&&val[i]>0)//如果該點已經走過就沒有必要再入隊了 另外根據題意要求最後一個點小於0 所以不能把小於0的點入隊                   {                       que.push(i);                       vis[i]=1;                   }                                  }           }           vis[temp]=0;       }          }   int main()   {       int i,m,x,y,min,flag;       while(scanf("%d %d",&n,&m)!=EOF)       {           memset(map,0,sizeof(map));           while(m--)           {               scanf("%d %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;   }    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved