[ACM] HDU 5137 How many Maos Does the Guanxi Worth (remove one point to maximize the shortest path)

Source: Internet
Author: User
Tags cmath

How many Maos Does the Guanxi Worth

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

SOURCE2014ACM/ICPC Asia Guangzhou Station-re-match (thanks to Chinese laborers and PKU)


Problem Solving Ideas:

Test instructions is a network with n points (number 1-n), and M relationships, by default, there is always a way to reach N from 1, all we have to do is to remove a point other than 1 and N, see if we can make from 1 can not reach N, if not reachable, the output inf can be, If you remove any of the points, you can get from 1 to N, then output the largest shortest path in these shortest paths.

n Max is 30, enumerate each point (except 1 and N), delete this point to find the shortest path, you can find the value of n-2 shortest path, if there is a situation can not reach N, then output INF, otherwise output this n-2 shortest path value of the maximum value.

Started by the one-way side do, WA two or three times, and then looked again test instructions, the result should be two-way side can .... Make sure you understand test instructions first!

Code:

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < algorithm> #include <vector> #include <queue> #include <stack> #include <map> #include < set> #include <cmath> #include <time.h> #include <iomanip> #include <cctype>using namespace STD; #define LL long longconst int maxn=32;const int Inf=0x3f3f3f3f;int n,m;//nodes, number of forward edges int mp[maxn][maxn];int DIS[MAXN];    BOOL Vis[maxn];bool ok;void Dijkstra (int start,int del) {memset (dis,inf,sizeof (dis));    memset (vis,0,sizeof (VIS));    dis[start]=0;        for (int i=1;i<=n;i++) {if (I==del) continue;        int k,min=inf;                for (int j=1;j<=n;j++) {if (Dis[j]<min&&!vis[j]) {min=dis[j];            K=j;        }} vis[k]=1;            for (int j=1;j<=n;j++) {if (J==del) continue; if (dis[k]+mp[k][j]<dis[J]) Dis[j]=dis[k]+mp[k][j]; }}}int Main () {while (scanf ("%d%d", &n,&m)!=eof&& (n| |        m)) {int from,to,w;        Memset (Mp,inf,sizeof (MP));            for (int i=1;i<=m;i++) {scanf ("%d%d%d", &from,&to,&w);                if (W<mp[from][to]) {mp[from][to]=w;            Mp[to][from]=w;        }} ok=0;//judge to remove a point can not make the graph is not connected int ans=-1; for (int i=2;i<=n-1;i++) {Dijkstra (1,i);//The point where the enumeration is removed, the shortest path if (dis[n]==inf) ok            = 1;        if (Ans<dis[n])//To find the longest shortest path ans=dis[n];            } if (ok==1) {printf ("inf\n");        Continue    } printf ("%d\n", ans); } return 0;}


Let's start with DFS to determine if we can get from 1 to N.

#include <algorithm> #include <vector> #include <queue> #include <stack> #include <map># Include <set> #include <cmath> #include <time.h> #include <iomanip> #include <cctype> using namespace std; #define LL long longconst int maxn=32;const int Inf=0x3f3f3f3f;int n,m;//nodes, number of forward edges int MP[MAXN][MAXN];    int Dis[maxn];bool vis[maxn];bool ok;void dijkstra (int start,int del) {memset (dis,inf,sizeof (dis));    memset (vis,0,sizeof (VIS));    dis[start]=0;        for (int i=1;i<=n;i++) {if (I==del) continue;        int k,min=inf;                for (int j=1;j<=n;j++) {if (Dis[j]<min&&!vis[j]) {min=dis[j];            K=j;        }} vis[k]=1;            for (int j=1;j<=n;j++) {if (J==del) continue;        if (Dis[k]+mp[k][j]<dis[j]) dis[j]=dis[k]+mp[k][j];    }}}void dfs (int s,int k) {vis[s]=1; for (int i=1;i<=n;i++) {if (!vis[i]&&i!=k&&mp[s][i]!=inf) DFS (I,K); }}int Main () {while (scanf ("%d%d", &n,&m)!=eof&& (n| |        m)) {int from,to,w;        Memset (Mp,inf,sizeof (MP));            for (int i=1;i<=m;i++) {scanf ("%d%d%d", &from,&to,&w);                if (W<mp[from][to]) {mp[from][to]=w;            Mp[to][from]=w;        }} ok=0;//judge to remove a point can not make the graph is not connected int ans=-1;            for (int i=2;i<=n-1;i++) {memset (vis,0,sizeof (VIS));            DFS (1,i);                if (vis[n]==0) {ok=1;            Break        } Dijkstra (1,i);//The point that the enumeration is removed to find the shortest path if (Ans<dis[n])//To find the longest short circuit of the ans=dis[n];            } if (ok==1) {printf ("inf\n");        Continue    } printf ("%d\n", ans); } return 0;}


[ACM] HDU 5137 How many Maos Does the Guanxi Worth (remove one point to maximize the shortest path)

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.