图论类型各题总结-acm.doc
hdu4318 Power transmission 最短路 当数据很大的时候的解决办法 一道题目的解题全过程记录最短路 解决大数据模拟链表Power transmissionTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 663 Accepted Submission(s): 231Problem DescriptionThe project WestEast power transmission is famous around the world。 It transmits the electricity from western areas to east China. There are many nodes in the power system。 Each node is connected with several other nodes in the system by cable。 Power can be only transmitted between two connected nodes。 For each node, it can't send power to two or more other nodes at the same time。As we have all known, power will be loss during the transmission。 Bob is the chief engineer of the project。 He wants to build a transmission line which send power from one node to another node and minimize the power loss at the same time。 Now he asks you to help him solve the problem。 InputThere are several test cases. For each test case, the first line contains an integer N (0 < N 50000) which represents the number of nodes in the power system。 Then there will be N groups of data following。 For the i-th(0 < i N) group, the first line is an integer ki (ki 50), which means the node i is connected with ki nodes. The rest of the ith group data are divided into ki lines. Each line contains an integer ai (0 < ai N, ai i) and an integer bi (0 bi 100), which represents power can be transmitted from node i to ai and will loss bi% while transmitting。 The last line of input data contains three integers separated by single spaces。 The first one is s, the second is t (0 s, t N), and the third is the total power M (0 M 106) at node s. OutputFor each test case, output the minimum of loss power while transmitting from node s to node t。 The result should be printed with two digits to the right of the decimal point. If power cannot be transmitted from node s to node t, output “IMPOSSIBLE!" in a line. Sample Input 422 503 7021 304 2021 104 4001 4 100Sample Output60.00HintIn the sample, the best transmission line is 1 -> 2 4, loss power is 100 50 + 100 * (100-50)20 = 60.00MicrosoftInternetExplorer402DocumentNotSpecified7。8Normal0题意:多个点 从一个点往另一个点运输天然气 但是从某点到另外一点是会有损耗的 题中输入的即为损耗的百分数 问从点s到t最少损耗多少 分析: 很明显是最短路问题 但是有个比较卡人的地方 就是最多会有50000个点 数据太大 用map数组存不下 会爆掉 我们考虑到用链表 就用了数组去模拟链表 保存从当前点能到达的点队长把写解题报告的任务光荣的交给了我 但是我对看代码表示相当的纠结 所以就自己去写了个 好理解 把自己的贴上 /#include<stdio。hinclude<string。h>struct hahaint son55; /记录从当前点到其它能走到的点double son_val55; /记录从当前点到其它能走到的点的消耗int cnt;/记录存取的儿子个数a50100;int n,flag,s,t,M,used50100;double min50100;double find_short()/模拟以前数据比较小的时候的模板 int i,j,pos,cnt; double mm; memset(used,0,sizeof(used); for(i=1;i=n;i+) mini=1; cnt=as.cnt; while(cnt) minas。soncnt=as。son_valcnt; useds=1; mins=0; for(i=2;i=n;i+) mm=1; for(j=1;j<=n;j+) if(!usedjmmminj) pos=j; mm=minj; if(mm=1) flag=1;return 0; usedpos=1; cnt=apos。cnt; for(j=0;j<cnt;j+) if(!usedapos。sonj&minpos+(1。0-minpos)*apos.son_valjminapos.sonj) minapos。sonj=minpos+(1.0minpos)apos。son_valj; if(pos=t) return mint;/这里很重要 不加会超时的哦 return mint;int main()int i,m,x,y;double val,k;while(scanf("d",n)!=EOF)flag=0;for(i=1;i=n;i+) ai。cnt=0;for(i=1;i<=n;i+)scanf("%d",&m);while(m-)scanf("d d",&x,&y);val=y/100.0;ai.sonai.cnt=x;ai。son_valai.cnt+=val; scanf(”d %d d”,s,t,M);k=find_short();if(flag=1) printf(”IMPOSSIBLE!n”);continue;printf("。2lfn”,kM);return 0;/hdu1596 find the safest road 最短路也能求最大值分类: 图论 201207-27 23:21177人阅读评论(0)收藏编辑删除find the safest roadTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2477 Accepted Submission(s): 978Problem DescriptionXX 星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条从u 到 v 的通道P 的安全度为Safe(P) = s(e1)*s(e2)s(ek) e1,e2,ek是P 上的边 ,现在8600 想出去旅游,面对这这么多的路,他想找一条最安全的路。但是8600 的数学不好,想请你帮忙 _ Input输入包括多个测试实例,每个实例包括:第一行:n。n表示城市的个数n=1000;接着是一个nn的矩阵表示两个城市之间的安全系数,(0可以理解为那两个城市之间没有直接的通道)接着是Q个8600要旅游的路线,每行有两个数字,表示8600所在的城市和要去的城市 Output如果86无法达到他的目的地,输出"What a pity!",其他的输出这两个城市之间的最安全道路的安全系数,保留三位小数. Sample Input3 1 0。5 0。50.5 1 0。40.5 0。4 131 22 31 3 Sample Output0。5000。4000。500 Authorailyanluincludestdio.h>#include<string。hint n,used1005;double map10051005,min1005;void find_short(int s)int i,j,pos;double mm;memset(used,0,sizeof(used);for(i=1;i=n;i+)mini=mapsi;mins=1;useds=1;for(i=2;i<=n;i+)pos=0;mm=-1;for(j=1;j=n;j+)if(!usedj&mmminj)pos=j;mm=minj;usedpos=1;if(mm=1) break;for(j=1;j<=n;j+)if(!usedj&minposmapposj>minj)minj=minpos*mapposj;int main()int i,j,m,s,e;while(scanf(”d”,n)!=EOF)for(i=1;i=n;i+)for(j=1;j<=n;j+)scanf("lf",&mapij);scanf("d",&m);while(m-) scanf("%d d",&s,e);find_short(s);if(mine!=0) printf("%.3lfn",mine);else printf(”What a pity!n”);return 0;hdu1358 Minimum Transport Cost 按字典序输出最短路径hdu1385分类: 图论 20120727 23:2339人阅读评论(0)收藏编辑删除Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3923 Accepted Submission(s): 989Problem DescriptionThese are N cities in Spring country。 Between each pair of cities there may be one transportation track or none。 Now there is some cargo that should be delivered from one city to another. The transportation fee consists of two parts:The cost of the transportation on the path between these cities, anda certain tax which will be charged whenever any cargo passing through one city, except for the source and the destination cities。You must write a program to find the route which has the minimum cost。 InputFirst is N, number of cities. N = 0 indicates the end of input.The data of path cost, city tax, source and destination cities are given in the input, which is of the form:a11 a12 。.。 a1Na21 a22 。 a2N。.。.。aN1 aN2 。. aNNb1 b2 。.。 bNc de f.。g hwhere aij is the transport cost from city i to city j, aij = 1 indicates there is no direct path between city i and city j. bi represents the tax of passing through city i. And the cargo is to be delivered from city c to city d, city e to city f, 。.。, and g = h = 1. You must output the sequence of cities passed by and the total cost which is of the form: OutputFrom c to d :Path: c->c1->.。.。>ck-dTotal cost : .。.。.。.From e to f :Path: e->e1-。.。.。.。-ek->fTotal cost : 。.。Note: if there are more minimal paths, output the lexically smallest one。 Print a blank line after each test case。 Sample Input50 3 22 -1 43 0 5 -1 122 5 0 9 20-1 1 9 0 44 1 20 4 05 17 8 3 11 33 52 41 10 Sample OutputFrom 1 to 3 :Path: 1>5>4-3Total cost : 21From 3 to 5 :Path: 3->4-5Total cost : 16From 2 to 4 :Path: 2-1->5->4Total cost : 17#includestdio。h>includestring.h#define size 1000int valsize,mapsizesize,usedsize,minsize,n,startsize,endsize,flag,presize;char s11300,s21300;int ok(int n1,int n2,int s)/比较字典序大小int i,k; i=0; s1i+=n1+'0; k=pren1; while(k!=s) s1i+=k+0;k=prek; s1i+=s+0; s1i='0' strrev(s1);/把字符串翻转过来 i=0; s2i+=n1+0; k=n2;/因为依然是n1(j)为最后一个 通过n2(pos)架桥 while(k!=s) s2i+=k+'0';k=prek; s2i+=s+0; s2i=0'; strrev(s2); if(strcmp(s2,s1)0)return 1; else return 0;void find_short(int s)int i,j,mm,pos;memset(used,0,sizeof(used));for(i=1;i=n;i+)mini=mapsi;if(mapsi!=999999999) /这一步居然忘记了 阿弥陀佛 记住这个千万不能少prei=s;mins=0;useds=1;for(i=2;i=n;i+)mm=999999999;for(j=1;j=n;j+)if(!usedj&mm>minj)pos=j;mm=minj;if(mm=999999999) break;usedpos=1;for(j=1;j=n;j+)if(!usedj&minpos+mapposj+valposminj)minj=minpos+mapposj+valpos;prej=pos;elseif(!usedjminpos+mapposj+valpos=minj)if(ok(j,pos,s) prej=pos;/如果pos之前的字典序较小那么更新void print(int s,int e)int asize,i,k;k=0;ak+=e;i=e;while(s!=i)i=prei;ak+=i;printf(”Path: ”);for(i=k1;i>0;i)printf(”d-",ai);printf(”dn",a0);int main()int i,j,s,e,num;while(scanf(”d”,&n)&n!=0)num=0;for(i=1;i=n;i+)for(j=1;j=n;j+)scanf("d”,mapij);if(mapij=-1) mapij=999999999;for(i=1;i=n;i+)scanf(”d”,&vali);while(1)scanf("%d d”,s,e);if(s=1&e=1) break;startnum=s;endnum+=e;for(i=0;inum;i+)flag=0;find_short(starti);printf("From d to d :n”,starti,endi);if(starti!=endi)print(starti,endi);else printf(”Path: dn”,starti);printf(”Total cost : dn",minendi);printf(”n”);return 0;/*记录路径思路就是加入一个pre数组 时时更新每个点前面的点是什么 另外不要忘记从始点出发到i的所有的pre都要赋值初始点本题要求按字典序找出最短的路径 一开始我感到很恐惧 不知道怎么做 其实心里没有必要那么害怕 只要尝试着一点一点的去做不要不敢尝试就退却 把大问题分解成一个一个小问题 硬着头皮也要上 (看了别人代码才A了)/hdu1548 A strange lift 不错的变相最短路考察分类: 图论 20120727 23:2412人阅读评论(0)收藏编辑删除A strange liftTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5507 Accepted Submission(s): 2009Problem DescriptionThere is a strange lift。The lift can stop can at every floor as you want, and there is a number Ki(0 = Ki = N) on every floor。The lift have just two buttons: up and down.When you at floor i,if you press the button ”UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button ”DOWN” , you will go down Ki floor,i。e,you will go to the i-Ki th floor。 Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5。Begining from the 1 st floor,you can press the button ”UP”, and youll go up to the 4 th floor,and if you press the button "DOWN”, the lift cant do it, because it can't go down to the -2 th floor,as you know ,the 2 th floor isnt exist.Here comes the problem: when you is on floor A,and you want to go to floor B,how many times at least he havt to press the button ”UP" or ”DOWN”? InputThe input consists of several test cases。,Each test case contains two lines.The first line contains three integers N ,A,B( 1 = N,A,B <= 200) which describe above,The second line consist N integers k1,k2,。kn。A single 0 indicate the end of the input. OutputFor each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B。If you can't reach floor B,printf ”1"。 Sample Input5 1 53 3 1 2 50 Sample Output3/题意是在某一层的楼梯 电梯只能上或者下相应的层数 从a-b最少要上下多少次如果不能从a到b则输入1 变形的最短路问题 层数就是点 看第几层和第几层有路此题比较简单 /#includestdio.h>#includestring。h>int map205205,used205,min205,n,start,end;void init()int i,j;for(i=1;i<=n;i+)for(j=1;j<=n;j+) if(i!=j) mapij=999999999;else map00=0;void find_short()int i,j,mm,pos;memset(used,0,sizeof(used));for(i=1;i=n;i+)mini=mapstarti;minstart=0; usedstart=1;for(i=2;i=n;i+)pos=0;mm=999999999;for(j=1;j=n;j+)if(!usedjmmminj)pos=j;mm=minj;usedpos=1;if(mm=999999999) break;for(j=1;j<=n;j+)if(!usedjminpos+mapposjminj)minj=minpos+mapposj;int main()int i,d;while(scanf(”%d”,n)!=EOF)if(n=0) break;scanf(”d d",&start,&end);init();for(i=1;i=n;i+)scanf(”d”,d);if(i-d=1) mapiid=1;if(i+d<=n) mapii+d=1;find_short();if(minend=999999999) printf(”1n");elseprintf(”dn",minend);return 0;A Walk Through the ForestTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2963 Accepted Submission(s): 1075Problem DescriptionJimmy experiences a lot of stress at work these days, especially since his accident made working difficult。 To relax after a hard day, he likes to walk home。 To make things even nicer, his office is on one side of a forest, and his house is on the other。 A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable。The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house。 He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take. InputInput contains several test cases followed by a line containing 0。 Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2。 The first line of each test case gives the number of intersections N, 1 < N 1000, and the number of paths M。 The following M lines each contain a pair of intersections a b and an integer distance 1 d 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses。 There is at most one path between any pair of intersections。 OutputFor each test case, output a single integer indicating the number of different routes through the forest。 You may assume that this number does not exceed 2147483647 Sample Input5 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 13 7 17 4 17 5 16 7 15 2 16 2 10 Sample Output24大致题意:他的办公室用1表示,家用2 表示,从1到2,中间可能会经过其它节点,而该节点可走的原则是:假设他此时在A处,B与其相邻,只有当B到2 路线中存在一条比A到2 的任意一条路径都短的