Hdoj 5137 How many Maos Does the Guanxi Worth "enumerate delete points + shortest circuit"

Source: Internet
Author: User

How many Maos Does the Guanxi WorthTime limit:2000/1000 MS (java/others) Memory limit:512000/512000 K (java/others) Total Submission (s): 847 Accepted Submission (s): 322

Problem Description "Guanxi" is a very important word in Chinese. It kind of means "relationship" or "contact". Guanxi can is based on friendship, but also can is built on money. So Chinese often say "I don ' t has one Mao (0.1 RMB) guanxi with you." or "the guanxi between them was naked money guanxi." It is said that the Chinese society are a guanxi society so can see guanxi plays a very important role in many things .

Here's an example. In many cities in China, the government prohibit the middle school entrance examinations in order to relief studying Burde N of primary school students. Because there is no clear and strict standard of entrance, someone could make their children enter good middle schools throu GH Guanxis. Boss Liu wants to send he kid to a middle school by guanxi this year. So he and find out his guanxi net. Boss Liu ' s guanxi net consists of N people including Boss Liu and the schoolmaster. In this net, the persons who have a guanxi between them can help each other. Because Boss Liu is a big money (in Chinese 中文版, a "big money" means one who had a lot of money) and have little friends , his guanxi net was a naked money guanxi net--it means so if there is a guanxi between A and B and a helps B, a must g ET paid. Through his guanxi net, Boss Liu could ask a to help him, then a could ask B for help, and then B could ask C for help ... If the request finally reaches the schoolmaster, Boss Liu ' s Kid'll is accepted by the middle school. Of course, all helpers including the schoolmaster is paid by Boss Liu.

You hate boss Liu and your want to undermine boss Liu ' s plan. All the can do are to persuade a person in Boss Liu's guanxi net to reject any request. This person can is any one, but can ' t be Boss Liu or the schoolmaster. If you can ' t do boss Liu fail, you want boss Liu to spend as much money as possible. You should figure out this after you had done your best, how much at least must Boss Liu spend to get what he wants. Please note this if you do nothing, Boss Liu'll definitely succeed.
Inputthere is several test cases.

For each test case:

The first line contains the integers n and M. N means that there is n people in Boss Liu ' s guanxi net. They is numbered from 1 to N. Boss Liu is no. 1 and the schoolmaster is No. N. m means that there be M guanxis in Boss L IU ' s guanxi net. (3 <=n <= 3 <= M <= 1000)

Then M lines follow. Each line contains three integers a, B and C, meaning that there is a guanxi between A and B, and if A asks B or B asks a For help, the helper would be paid C $ by Boss Liu.

The input ends with N = 0 and M = 0.

It's guaranteed that Boss Liu's request can reach the schoolmaster if you don't try to undermine his plan.
Outputfor Each test case, the output of the minimum money Boss Liu had to spend after you had done your best. If Boss Liu would fail to send his kid to the middle school, print "INF" instead.
Sample Input
4 51 2 31 3 71 4 502 3 43 4 23 21 2 302 3 100 0

Sample Output
50Inf

Test instructions: There are n points and M bar no forward edges. Ask you to delete a point (not a point 1 and Point N), the shortest path from 1 to n is the maximum number. If you delete a point from point 1 cannot reach point N, output INF.

Idea: Enumerate the deletion point, Dijkstra to find the shortest way. If there is a shortest path, update the maximum value, otherwise jump out of the output INF.

AC Code:
#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 100#define INF 0x3f3f3fusing    namespace Std;int map[maxn][maxn];int N, M;bool vis[maxn];int dist[maxn];int Dijkstra () {int Next, Min;        for (int i = 1; I <= N; i++) {vis[i] = false;    Dist[i] = Map[1][i];    } Vis[1] = true;        for (int i = 2; I <= N; i++) {Min = INF;  for (int j = 1; J <= N; j + +) {if (!vis[j] && min > Dist[j]) {min                = Dist[j];            Next = j;        }} Vis[next] = true; for (int j = 1; J <= N; j + +) {if (!vis[j] && map[next][j]! = INF) Dist[j] = min        (Dist[j], Dist[next] + map[next][j]); }} return dist[n];}        void Init () {for (int i = 1; I <= N; i++) {map[i][i] = 0;    for (int j = 1; j < I; j + +) Map[i][j] = map[j][i] = INF;    }}void Getmap () {int x, y, Z; for (iNT i = 1; I <= M;        i++) {scanf ("%d%d%d", &x, &y, &z);    if (Map[x][y] > z) map[x][y] = map[y][x] = Z;    }}int rec[maxn][maxn];//record function void Solve () {int ans = Dijkstra ();            BOOL Flag = false;//Determines whether the for (int i = 2; I <= N-1; i++) {for (int j = 1; J <= N; j + +) {            REC[I][J] = Rec[j][i] = Map[i][j];        MAP[I][J] = map[j][i] = INF;            } if (Dijkstra () = = INF)//successfully destroyed {flag = true;        Break        } ans = max (Dijkstra (), ans);    for (int j = 1; J <= N; j + +) Map[i][j] = map[j][i] = Rec[i][j];    } if (flag)//succeeded in destroying printf ("inf\n"); else printf ("%d\n", ans);} int main () {while (scanf ("%d%d", &n, &m), n| |        M) {init ();        Getmap ();    Solve (); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 5137 How many Maos Does the Guanxi Worth "enumerate delete points + shortest circuit"

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.