UVA 10985 Rings'n'Ropes

最短路 参考了Staingger的博客

感觉DP的状态记录还是有毛病。可以DFS寻找结果也。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 150
const int INF = 0x3f3f3f3f ;
int dp[MAXN][MAXN];
bool vis[MAXN][MAXN];
int N,M,ans;
void init()
{
    scanf("%d%d",&N,&M);
    memset(dp,0x3f,sizeof(dp));
    while (M--)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        dp[u][v] = dp[v][u] = 1;
    }
    for (int k = 0; k < N; k++)
        for (int i = 0; i < N; i++)
          for (int j = 0; j < N; j++)
           dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]);
    for (int i = 0; i < N; i++) dp[i][i] = 0;
}
int calcu(int s, int t)
{
    int res[MAXN],cas = 0,cnt = 0;
    for (int k = 0; k < N; k++)
        if (dp[s][k] + dp[k][t] == dp[s][t]) res[cas++] = k;
    for (int i = 0; i < cas; i++)
        for (int j = i + 1; j < cas; j++)
    {
        int a = res[i] , b = res[j];
        if(dp[a][b] == 1 && dp[s][a] != dp[s][b])
            cnt++;
    }
    return cnt;
}
void slove()
{
    for (int i = 0; i < N; i++)
        for (int j = i + 1; j < N; j++)
    {
        if (dp[i][j] != INF)
            ans = max(ans,calcu(i,j));
    }
}
int main()
{
    //freopen("sample.txt","r",stdin);
    int T,kase = 1;
    scanf("%d",&T);
    while (T--)
    {
        init();
        ans = 0;
        slove();
        printf("Case #%d: %d\n",kase++,ans);
    }
    return 0;
}

UVA 10985 Rings'n'Ropes

时间: 2024-11-13 15:19:50

UVA 10985 Rings'n'Ropes的相关文章

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

The Postal Worker Rings Once(UVA 117)最短路径—SPFA算法+邻接表

The Postal Worker Rings Once From:UVA, 117 Time Limit: 3000 MS Background Graph algorithms form a very important part of computer science and have a lineage that goes back at least to Euler and the famous Seven Bridges of K?nigsberg problem. Many opt

uva 1335 Beijing Guards(二分)

uva 1335 Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and finally the Outer City Wall. Most of these walls were demolished in the 50s and 60s to make way

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica