A __dfs&&bfs a good question of Dfs

Source: Internet
Author: User
There are n Doge planets in the Doge space. The Conqueror of Doge "is Super Doge", who's going to inspect his Doge in all Army Doge. The inspection starts from Doge Planet 1 where DOS (Doge Olympic statue) is built. It takes Super Doge exactly T xy 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. More specifically, he would the "visit" Doge Planet x at the time no later than X. 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 and inspect his Doge Army we can ignore it.

There are multiple test cases. Please process till EOF.
Each test case contains several lines. The ' a ' 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 T XY. Then follows a single line containing n-1 Integers:deadline 2 to Deadline N.
All numbers are guaranteed is non-negative integers smaller or than to one equal. N is guaranteed to am no less than 3 and no more than 30.

If Some deadlines can is fulfilled, please output "-1" (which means the Super Doge would say "wow!" So slow! Such delay! Much anger! . . . ", but you don't 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 8 4 0 2 3 3 2 0 3 3 2 3 0 3 2 3 3 0 2
3 3
Sample Output

1
The main idea of the topic is that a matrix of n multiplied by N is D,DXY to represent the distance from X to Y, then move from 0, each point will go through, and the time to reach each point must be within the limit, and each point can be repeated to reach the minimum time required to complete all points, if within the range of conditions can not walk all points, On the output-1.

First get the title, think of is BFS, of course, the smallest path, but think about it is not right, so I think a few times, today all afternoon thinking about this problem, the results were not thought out, but a period of time or to give me a clue, is to first find from one point to another point of the minimum distance, (Because the first give is not the minimum distance, it is possible to turn back later than the direct arrival of the short) is only the shortcomings of things, so still can not solve the problem, even after using a very violent method is also tle.

Later, when I couldn't figure out the solution, I found the solution of the minimum distance for each point, and it was very simple, as follows:

for (int k=0;k<n;k++) for (int
	i=0;i<n;i++) for
               	 (int j=0;j<n;j++)
                   	 s[i][j]=min (s[i][j],s[i][k]+s K [j]);
If there is a shorter path than the direct one, there must be a short distance between only one fold and one point, so that the shortest path can be found as long as the traversal is reached.

Or another way to understand the idea: first select a point, and then find another point, you can search from this point to another point to fold to the selected point of all the situation, find the smallest, and then replace the selected points, and then search, you can traverse all cases.

The next step is direct Dfs.

The code is as follows:

#include <stdio.h> #include <iostream> #include <cstring> #include <stdlib.h> #include <
algorithm> #include <queue> #include <math.h> #define MAX #define INF 1e8 using namespace std;
int n;//n number int line[max];//to each point the time limit int s[max][max];//input matrix int digi[max];//Mark whether each point goes through, because after finding the shortest path, there is no point to repeat the condition.
    int ans;//answer void dfs (int a,int tim,int t)//Now point A, now time Tim, before reaching each point time and t {int count1=0;
        for (int i = 1;i < n;i++) {if (digi[i]==0) count1++;
    if (Digi[i]==0&&tim>line[i]) return;
    }//pruning if (Count1&&t+tim*count1>ans) return;
    else if (!count1) {ans=min (T,ans);
            for (int i=1;i<n;i++) {if (I!=a&&digi[i]==0&&tim+s[a][i]<=line[i]) {
            Digi[i]=1;
            DFS (I,tim+s[a][i],t+tim+s[a][i]);
        digi[i]=0;
 int main (void) {while (scanf ("%d", &n)!=eof) {       for (int i = 0;i < n;i++) {for (int j = 0;j < n;j++) {scanf
            ("%d", &s[i][j]);
                    for (int k=0;k<n;k++) for (int i=0;i<n;i++) for (int j=0;j<n;j++) S[i][j]=min (S[i][j],s[i][k]+s[k][j])//Find the minimum for a point to each point for (int i = 1;i < n;i++) SC
        ANF ("%d", &line[i]);
        Ans=inf;
        DFS (0,0,0);
        if (Ans==inf) cout<<-1<<endl;
    else cout<<ans<<endl;
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.