hdu 2452 Navy maneuvers 记忆化搜索

Navy maneuvers

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 399    Accepted Submission(s): 159

Problem Description
In times of peace, various countries have held regular maneuvers to maintain military’s vigilance. There is a navy fleet in a certain country which also starts a new round imaginary naval battle.

At the maneuver stage, the admiral intends to assess the combat effectiveness of two warships, “Victory” and “Glory”, so he lets two warships carry out countering exercises. Both of the warship commanders are young and promising, who graduated from naval academy as outstanding students. Not only have they had rich navigation direction experiences, but also have profound scientific knowledge, especially in mathematics.

The admiral appoints one marine area dotted with many islets. Suppose all these islets are occupied by the enemy, and there are positive integers of enemy firebases. The hypothetical exercise situations given by the admiral and the rule of the competition are as follows:

(1) All the occupied islets are connected. There are some routes among these islets, but the route from one islet to another islet is unidirectional. In other words, if we take an islet as a node and an islet route as an edge, then we will get a directed non-cyclic connected graph.
(2) There is a unique 1st islet in the graph. If we start from this islet, we can reach any other islet. (maybe the 1st islet is not the islet which is numbered 1)
(3) At the beginning of the maneuver, two warships simultaneously sail to the 1st islets and eliminate all enemy firebases together.
(4) The two warships, “Victory” and “Glory” take turns to navigate and exchange as soon as they arrive at an islet, then they move forward together. But each time they can only go along the unidirectional route, sail to the islet directly connected to the current, and eliminates all the enemy firebases on the islet. By the way, when start from 1st islet, “Victory” first navigates.
(5) Because each route is unidirectional, and graph is non-cycle, therefore the maneuver terminates when the two warships fail to go on navigating.
(6)When the maneuver ends, sum the numbers of eliminated enemy firebases on the routing path. If the number is greater than or equal to certain number f assigned by the admiral, then “Victory” wins. Otherwise, “Glory” wins.

The warship commanders are both mathematicians. After being assigned to such task, they see it is a Graph Theory problem. On the first simple directed non-cyclic connected graph, each node has a certain positive integer,it indicates the enemy firebases. The assignment is that two captains take turn to move forward along the directed edge until they are unable to do so. Then sum the total positive integers of the nodes on the routing path. Compare the number with the certain number f, and decides the final winning or losses.

Therefore, when it is the time for their own navigation, they are supposed to choose the route to win the final success.
 

 

Input
There are several test cases, in each case there are three positive integers n, m and f in first line. n indicates there are n (1< n <10000 ) nodes in the graph. The node serial number is arranged from 1 to n. m indicates that there are m edges in the graph. The following line has n positive integers, which indicate in sequence the positive integers in the nodes. Finally, there are m lines, and each line has two positive integers a, b (1< = a, b< =n), indicating there is a directed edge from the a node to the b node. Input is terminated by the end of file.
 

 

Output
To each group of the test case, if “Victory” wins, then output “Victory”. Otherwise, output “Glory”. The output each case takes up one line.
 

 

Sample Input
  
  
4 4 7 2 2 2 2 4 2 2 1 4 3 3 1 4 5 6 1 2 3 4 1 2 1 3 1 4 2 3 4 3
 

 

Sample Output
  
  
Glory Victory
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int inf=(1<<30);
const int N=10010;
vector<int> G[N];
int n,m,f,in[N],out[N],enermy[N];
int kill[N][2];//1 Vector 0 Glory
//int vis[N];
int dfs(int now,int mark)
{
    if(mark==1)//由 Vector 做决定
    {
        if(kill[now][1]!=-1) return kill[now][1];
        int big=0;
        for(int i=0,len=G[now].size();i<len;i++)
        {
            //if(vis[G[i]]) continue;
            int t=dfs(G[now][i],0);
            if(t>big) big=t;
        }
        kill[now][1]=big+enermy[now];
        return kill[now][1];
    }
    else
    {
        if(kill[now][0]!=-1) return kill[now][0];
        int sma=inf;
        for(int i=0,len=G[now].size();i<len;i++)
        {
            int t=dfs(G[now][i],1);
            if(t<sma) sma=t;
        }
        if(sma==inf) kill[now][0]=enermy[now];
        else kill[now][0]=sma+enermy[now];
        return kill[now][0];
    }
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&f)==3)
    {
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(kill,-1,sizeof(kill));
       // memet(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)  scanf("%d",&enermy[i]),G[i].clear();
        for(int i=1;i<=m;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            in[y]++,out[x]++;
            G[x].push_back(y);
        }
        int begin;//找第一个点
        for(int i=1;i<=n;i++) if(in[i]==0) {begin=i;break;}
       // vis[begin]=1;
        int cnt=dfs(begin,1);//1 Vector 0 Glory
        if(cnt>=f) printf("Victory/n");
        else printf("Glory/n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值