FZYZOJ-1144 Demon City

Source: Internet
Author: User

P1144--Demon City

Time limit: 1000MS memory limit: 131072KB

Status: Accepted Tags: graph theory-shortest circuit two minutes no

Description

God needs to create a warrior to destroy Satan, and the warrior must go through the demon City to fight Satan. There are streets in the demon city with m bars connecting n intersections (numbered 1 to N) and each street is one-way (that is, you cannot walk in the direction specified by the street), and there is no way to walk back to the place where you left off. At first, the warrior's Vitality (HP) was inithp, standing at the junction of 1th, while Satan was waiting for him at the nth intersection. There are many demons on every street, but there are also some streets that have been occupied by God-sent angels. When the warrior passes through the street connecting the I to the J, if the devil is occupying the street, then his HP doubles and then reduces l[i,j], and we remember it as a[i,j]=-l[i,j]; If an angel occupies the street, then his HP doubles and increases L[I,J], which we remember as a[ I,J]=+L[I,J]; If the street does not exist, then a[i,j]=0. If a warrior's hp<=0 in a moment, he will die. Because this warrior will be very invincible, when he sees Satan, as long as he is alive, he can destroy Satan in one breath, so God does not want his inithp to be too high.

task: given N,A[1..N,1..N], to find the smallest inithp, so that the warrior can live to see Satan.

Input Format

The first line has a positive integer n(3 n ), followed by the number of line I J is a[i,j] (An integer not exceeding 10000 in absolute value ).

Output Format

The minimum inithp of the output is obtained.

Sample Input
40-4 0-100 0 +3 00 0 0-140 0 0 0
Sample Output
4
Hint

HP is an integer.

Solving

Because the minimum value of HP is required, think of the two-point answer first. The absolute value of a[i][j] is not greater than 10000, then the two intervals are [1,10000]. As there will be at most only 100 points, so you can use deep search to determine. Note that since HP is constantly multiplying by two, the final value will be very large, how to solve it? It is not difficult to find, because the absolute value of a[i][j] is not greater than 10000, as long as HP is greater than or equal to 10000, then must be able to reach the end, so you can set 10000 as the INF, once HP exceeds the INF, it is set to INF.

Code

#include <cstdio> #include <iostream> #define MAXN 110#define SIZE 100000#define INF 10005using namespace std; Char B[1<<15],*s=b,*t=b;char getc () {return s==t&& (t= (s=b) +fread (B,1,1<<15,stdin), S==T)? 0:*S++ ;} #define ISD (c) (c>= ' 0 ' &&c<= ' 9 ') int Aa,bb;char ch;int Scan () {while (Ch=getc (),!ISD (CH) &&ch!= '-' ch== '-' aa=bb=0: (aa=ch-' 0 ', bb=1), while (Ch=getc (), ISD (CH)) aa=aa*10+ch-' 0 '; return BB?AA:-AA;} struct EDGE {int to, Next, W;}; int N;int A[MAXN][MAXN]; EDGE e[size];int Top, last[maxn];int Min (int a, int B) {return a < b? A:B;} void Add (int A, int B, int C) {++top; E[top]. to = B; E[top]. Next = Last[a]; E[top]. W = C; Last[a] = Top;} BOOL DFS (int HP, int X) {if (X = = N) return 1;for (int i = last[x]; i; i = e[i]. Next) {if (HP * 2 + e[i]. W > 0) {if (DFS (Min (HP * 2 + e[i). W, INF), E[i]. To) return 1;}} return 0;} int main () {n = scan (), for (int i = 1, i <= N; ++i) {for (int j = 1; j <= N; ++j) {A[i][j] = scan (); if (a[i][j]! = 0 ) Add (I, J, A[i][j]);}} int L = 1, R = Inf;while (L <= r) {int Mid = (L + R) >> 1;if (DFS (Mid, 1)) R = Mid-1;else L = Mid + 1;} printf ("%d\n", L);}

FZYZOJ-1144 Demon City

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.