HDOJ 5422-Rikka with Graph

HDOJ 5422-Rikka with Graph Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 144 Accepted Submission(s): 72 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has a non-direct graph with n vertices and m edges. The length of each edge is 1. Now he wants to add exactly an edge which connects two different vertices and minimize the length of the shortest path between vertice 1 and vertice n. Now he wants to know the minimal length of the shortest path and the number of the ways of adding this edge. It is too difficult for Rikka. Can you help her? Input There are no more than 100 testcases. For each testcase, the first line contains two numbers n,m(2≤n≤100,0≤m≤100). Then m lines follow. Each line contains two numbers u,v(1≤u,v≤n) , which means there is an edge between u and v. There may be multiedges and self loops. Output For each testcase, print a single line contains two numbers: The length of the shortest path between vertice 1 and vertice n and the number of the ways of adding this edge. Sample Input 2 1 1 2 Sample Output 1 1 Hint You can only add an edge between 1 and 2. Source BestCoder Round #53 (div.2) Recommend hujie | We have carefully selected several similar problems for you: 5426 5425 5424 5423 5422


水题。 如果原来就有边(1,n),则最短距离就是1,可以任选2个点构成一条新边,方案数$$C^2_n$$;如果原来没有边(1,n),则添加的新边就是(1,n),最短距离还是1,此时只有一种方案。 完整代码如下: [cpp] #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> using namespace std; int main() { int n, m; while (~scanf("%d %d", &n, &m)) { bool connected = false; int u, v; while (m–) { scanf("%d %d", &u, &v); if ((u == 1 && v == n) || (u == n&&v == 1)) connected = true; } if (connected) printf("1 %d\n", n*(n – 1) / 2); else printf("1 1\n"); } return 0; } [/cpp] 本代码提交AC,用时0MS,内存1216K。]]>

Leave a Reply

Your email address will not be published. Required fields are marked *