Bc Rikka with Graph

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 nn vertices and mm 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 nn. 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?



这个题目就是坑啊 语文没学好啊 语文没学好啊 语文没学好啊 

一开始想的是先求最短路 ,在对要连得点枚举一下 有更小的路长 那么最短路更新下 如果加边了最短路还是一样的话 那么就更新答案

蓝而 这是没用的 ,蓝而 只是会无限TLE而已。

后来突然脑洞一开, 诶  貌似只要按照题意, 1和n 一连最短路就是1 喔微笑  明白了这个 我就感受到了语文不好恶果, 错的不是我是这个世界微笑

这个知道了, 那莫如果一开始1 和 n 之间没有之间连起来 , 那莫最短里的方案数就是1 ,因为其他地方不管怎么连都会比之间连1和n大

如果1和n直接连通,那莫答案就是 把1到n之间每个方案都加起来,简直了 

看到我代码上面一堆乱七八糟的东西就知道我已开始想了什么。。。。。。。

include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int inf = 1000000;
struct Edge{
    int u,v;
};
struct A
{
    int id , c;
    A(int id, int c) : id(id) , c(c) {}
};
Edge g[200],rg[200];
int n,m;
int vis[110];
int dis[110];
int map1[110][110];
bool operator < (const A &a, const A &b)
{
    return a.c > b.c;
}
priority_queue < A > q;
void dijkstra(int mixx)
{
    for(int i = 1; i <= n; i++)
    {
        vis[i] = 0;
        dis[i] =inf;
    }
    dis[1] = 0;
    q.push(A(1,0));
    while(!q.empty())
    {
        int u = q.top().id; q.pop();
        if(vis[u]) continue;
        vis[u] = 1;
       // if( dis[u] > mixx) continue;
        for(int i = 1; i <= n; i++)
        {
            if(!vis[i] && dis[i] > dis[u] + map1[u][i])
            {
                dis[i] = dis[u] + map1[u][i];
                q.push(A(i,dis[i]));
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(i != j)
                map1[i][j] = inf;
                else
                    map1[i][j] = 0;
        int x,y;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d",&x,&y);
            map1[x][y] = 1;
            map1[y][x] = 1;
        }
        if(map1[1][n] != 1)
        {
            printf("1 1\n");
            continue ;
        }
        else
        {
            int ans = 0;
            for(int i = 1; i <= n; i++)
            {
                for(int j = i+1; j <= n; j++)
                 ans++;
            }
            printf("1 %d\n",ans);
        }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值