zoukankan      html  css  js  c++  java
  • HDU 4848

    Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

    Problem Description
    There are n Doge Planets in the Doge Space. The conqueror of Doge Space is Super Doge, who is going to inspect his Doge Army on all Doge Planets. The inspection starts from Doge Planet 1 where DOS (Doge Olympic Statue) was built. It takes Super Doge exactly Txy time to travel from Doge Planet x to Doge Planet y.
    With the ambition of conquering other spaces, he would like to visit all Doge Planets as soon as possible. More specifically, he would like to visit the Doge Planet x at the time no later than Deadlinex. He also wants the sum of all arrival time of each Doge Planet to be as small as possible. You can assume it takes so little time to inspect his Doge Army that we can ignore it.
     
    Input
    There are multiple test cases. Please process till EOF.
    Each test case contains several lines. The first line of each test case contains one integer: n, as mentioned above, the number of Doge Planets. Then follow n lines, each contains n integers, where the y-th integer in the x-th line is Txy . Then follows a single line containing n - 1 integers: Deadline2 to Deadlinen.
    All numbers are guaranteed to be non-negative integers smaller than or equal to one million. n is guaranteed to be no less than 3 and no more than 30.
     
    Output
    If some Deadlines can not be fulfilled, please output “-1” (which means the Super Doge will say “WOW! So Slow! Such delay! Much Anger! . . . ” , but you do not need to output it), else output the minimum sum of all arrival time to each Doge Planet.
    Sample Input
    4
    0 3 8 6
    4 0 7 4
    7 5 0 2
    6 9 3 0
    30 8 30
    4
    0 2 3 3
    2 0 3 3
    2 3 0 3
    2 3 3 0
    2 3 3
     
    Sample Output
    36
    -1
     
    Hint
    Explanation: In case #1: The Super Doge travels to Doge Planet 2 at the time of 8 and to Doge Planet 3 at the time of 12, then to Doge Planet 4 at the time of 16. The minimum sum of all arrival time is 36.

    首先Floyd算法得到任意两点间的最短时间;

    然后之间进行DFS,剪枝优化时间。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define MAXN 33
     5 #define INF 0x3f3f3f3f
     6 using namespace std;
     7 int d[MAXN][MAXN],ans;
     8 int n,deadline[MAXN],arrival_time[MAXN];
     9 bool vis[MAXN];
    10 void floyd()
    11 {
    12     for(int k=1;k<=n;k++)
    13     {
    14         for(int i=1;i<=n;i++)
    15         {
    16             for(int j=1;j<=n;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    17         }
    18     }
    19 }
    20 void dfs(int now,int cur_time,int sum_time,int cnt)  
    21 {  
    22     if(cnt==n)//走完了n个星球 
    23     {
    24         ans=min(ans,sum_time);//尝试更新答案,使得答案是到目前为止的最优解
    25         return;
    26     }
    27     
    28     if(sum_time + cur_time * (n-cnt) >= ans) return;
    29         //最优性剪枝:当前到达时间和 + 当前时间 * 还没去的星球 >= 到目前为止的最优解,剪掉 
    30         //      因为,到目前为止,还没去的星球,到达那里的时间必然大于等于当前时间
    31         
    32     for(int i=2;i<=n;i++) if(!vis[i] && cur_time>deadline[i]) return;
    33          //可行性剪枝:到目前为止,现在的时间大于未到达的星球的deadline,已经不满足要求,剪掉 
    34          
    35     for(int next=1;next<=n;next++)  
    36     {  
    37         if(now!=next && !vis[next] && cur_time+d[now][next]<=deadline[next])
    38         {  
    39             vis[next]=1;
    40             dfs(next,cur_time+d[now][next],sum_time+cur_time+d[now][next],cnt+1);
    41             vis[next]=0;
    42         }  
    43     }  
    44   
    45 } 
    46 int main()
    47 {
    48     while(scanf("%d",&n)!=EOF)
    49     {
    50         for(int i=1;i<=n;i++)
    51             for(int j=1;j<=n;j++)
    52             {
    53                 scanf("%d",&d[i][j]);
    54             }
    55             
    56         for(int i=2;i<=n;i++) scanf("%d",&deadline[i]);
    57         deadline[1]=INF;
    58         
    59         floyd(); 
    60         
    61         ans=INF;
    62         vis[1]=1;
    63         dfs(1,0,0,1);
    64         printf("%d
    ", ((ans==INF)?-1:ans) );
    65     }
    66 } 
  • 相关阅读:
    python实现快排算法,传统快排算法,数据结构
    pyaudio音频录制python
    python性能测试,请求QPS测试
    tensorflow如何切换CPU和GPU
    warmup预热学习率
    pytorch两种模型保存方式
    一个简单docker服务镜像的制作,手把手教你制作一个flask的docker容器服务镜像。
    threading的定时器模块,python,每间隔一段时间执行一次任务
    SVM简单分类的使用 sklearn机器学习
    Swoole从入门到入土(18)——WebSocket服务器[心跳ping]
  • 原文地址:https://www.cnblogs.com/dilthey/p/6815815.html
Copyright © 2011-2022 走看看