Hdu4126genghis Khan the Conqueror (minimum spanning tree + and collection)

Source: Internet
Author: User

Genghis Khan the ConquerorTime limit:10000/5000 MS (java/others) Memory limit:327680/327680 K (java/others)
Total submission (s): 1687 Accepted Submission (s): 501


Problem Descriptiongenghis Khan (Genghis Khan) (1162-1227), also known by his birth name Temujin (Khan) and temple name Taizu (Yuan zu), WA s the founder of the Mongol Empire and the greatest conqueror in Chinese history. After uniting many of the nomadic tribes in the Mongolian steppe, Genghis Khan founded a strong cavalry equipped by irony Discipline, sabers and powder, and he became to the very fearsome conqueror in the history. He stretched the empire that resulted in the conquest of the most of Eurasia. The following figure (Origin:wikipedia) shows the territory of Mongol Empire at that time.

Our stories are about Jebei Noyan (ZHE), who was one of the most famous generals in Genghis Khan ' s cavalry. Once his LEDs the advance troop to invade a country named Pushtuar. The Knights rolled up all the cities in Pushtuar rapidly. As Jebei Noyan ' s advance troop did not has enough soldiers, the conquest was temporary and vulnerable and he was waiting For the Genghis Khan ' s reinforce. At the meantime, Jebei Noyan needed to set up many guarders on the road of the country in order to guarantee that he Troo P in each city can send and receive messages safely and promptly through those roads.

There were N cities in Pushtuar and there were bidirectional roads connecting cities. If Jebei set up guarders on a road, it is totally safe to deliver messages between the both cities connected by the road. However setting up guarders on different road took different cost based on the distance, road condition and the residual a Rmed Power nearby. Jebei had known the cost of setting up guarders on each road. He wanted to guarantee, each of cities can safely deliver messages either directly or indirectly and the total cost W As minimal.

Things'll always get a little bit harder. As a sophisticated general, Jebei predicted this there would be one uprising happening in the country sooner or later whic H might increase the cost (setting-guarders) on exactly one road. Nevertheless he did not know which road would is affected, but only got the information of some suspicious road cost Chang Es. We assumed the probability of each suspicious case is the same. Since that after the uprising happened, the plan of Guarder setting should is rearranged to achieve the minimal cost, Jebe I Noyan wanted to know the new expected minimal total cost immediately based on current information.

Inputthere is no more than-test cases in the input.
For each test case, the first line contains integers N and M (1<=n<=3000, 0&LT;=M&LT;=NXN), demonstrating the Nu Mber of cities and roads in Pushtuar. Cities is numbered from 0 to N-1. In the all of the following M lines, there is three integers xi, Yi and Ci (ci<=107), showing this there is a bidirect Ional Road between Xi and Yi, while the cost of setting up guarders on this road is CI. We guarantee the graph is connected. The total cost of the graph was less or equal to 109.

The next line contains a integer Q (1<=q<=10000) representing the number of suspicious road cost changes. In the following Q-lines, each line contains three integers Xi, Yi and Ci showing, this cost of road (Xi, Yi) may Chang E to Ci (ci<=107). We guarantee that the road exists and Ci are larger than the original cost (we guarantee that there are at most one R Oad connecting, cities directly). Please note that the probability's suspicious road cost change is the same.

Outputfor each test case, output a real number demonstrating the expected minimal total cost. The result should is rounded to 4 digits after decimal point.

Sample Input
3 30 1 30 2 21 2 530 2 31 2 60 1 60 0

Sample Output
6.0000HintThe initial minimal cost are 5 by connecting City 0 to 1 and city 0 to 2. In the first suspicious case, the minimal total cost was increased to 6; The second case remains 5; The third case was increased to 7. As the result, the expected cost is (5+6+7)/3 = 6.

Source2011 Asia Fuzhou Regional Contest
#include <stdio.h> #include <vector> #include <algorithm>using namespace std;const int N = 3005;const    Double inf = 0x3f3f3f3f;struct edg{int u,v; Double C;};    struct to{int v; Double C;}; vector<to>tmap[n]; EDG edg[n];int N,treeedg[n][n];d ouble node[n]; EDG tedg[n*n];int fath[n];int cmp (EDG a,edg b) {return A.C&LT;B.C;}    int Findfath (int x) {if (x==fath[x]) return fath[x];    Fath[x]=findfath (Fath[x]); return fath[x];}    Double MST (int m) {double sum=0;    int k=0;        for (int i=0; i<m; i++) {int X=findfath (TEDG[I].U);        int Y=findfath (TEDG[I].V);            if (x!=y) {k++;            Treeedg[tedg[i].u][tedg[i].v]=treeedg[tedg[i].v][tedg[i].u]=k; edg[k].u=tedg[i].u; EDG[K].V=TEDG[I].V;            EDG[K].C=TEDG[I].C; Fath[x]=y;            SUM+=TEDG[I].C;        if (k==n-1) break; }} return sum;}    int main () {int m,q,a,b;    Double C,ANS,SUM,TC;    to SS; while (scanf ("%d%d", &n, &m) >0&&n+m!=0) {for (int i=0;i<=n;i++) {fath[i]=i;        for (int j=0;j<=n;j++) treeedg[i][j]=n;            } for (int i=0;i<m;i++) {scanf ("%d%d%lf", &a,&b,&c);            Tedg[i].u=a;            Tedg[i].v=b;        Tedg[i].c=c;        } sort (tedg,tedg+m,cmp);        Sum=mst (m);        scanf ("%d", &q);        ans=0;            for (int j=0;j<q;j++) {scanf ("%d%d%lf", &a,&b,&c);            if (treeedg[a][b]==n) ans+=sum;                else {for (int i=0;i<=n;i++) fath[i]=i;                       for (int i=1;i<n;i++) {if (treeedg[a][b]==i) { TC=EDG[I].C;                    Continue                    } int X=findfath (EDG[I].U);                    int Y=findfath (EDG[I].V);               Fath[x]=y; } int flag=0;                        for (int i=0;i<m&&tedg[i].c<c;i++) {if (treeedg[tedg[i].u][tedg[i].v]!=n)                        Continue                        int X=findfath (TEDG[I].U);                        int Y=findfath (TEDG[I].V);                         Fath[x]=y;                         X=findfath (a);                        Y=findfath (b);                        if (x==y) {ans=ans+sum-tc+tedg[i].c; flag=1; break;            }} if (flag==0) ans=ans+sum-tc+c;    }} printf ("%.4lf\n", ans/(q*1.0)); }}


Hdu4126genghis Khan the Conqueror (minimum spanning tree + and collection)

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.