2019ACM南京网络赛 Holy Grail

题目链接: https://nanti.jisuanke.com/t/41305

SPFA 或 Bellman-Ford模板题(存在负权路径)

Description

1000ms 262144K

As the current heir of a wizarding family with a long history,unfortunately, you find yourself forced to participate in the cruel Holy Grail War which has a reincarnation of sixty years.However,fortunately,you summoned a Caster Servant with a powerful Noble Phantasm.When your servant launch her Noble Phantasm,it will construct a magic field,which is actually a directed graph consisting of n vertices and m edges.More specifically,the graph satisfies the following restrictions :

1 Does not have multiple edges(for each pair of vertices x and y, there is at most one edge between this pair of vertices in the graph) and does not have self-loops(edges connecting the vertex with itself).
2 May have negative-weighted edges.
3 Does not have a negative-weighted loop.
4 n<=300 , m<=500.

Currently,as your servant’s Master,as long as you add extra 6 edges to the graph,you will beat the other 6 masters to win the Holy Grail.

However,you are subject to the following restrictions when you add the edges to the graph:

1 Each time you add an edge whose cost is c,it will cost you c units of Magic Value.Therefore,you need to add an edge which has the lowest weight(it’s probably that you need to add an edge which has a negative weight).
2 Each time you add an edge to the graph,the graph must not have negative loops,otherwise you will be engulfed by the Holy Grail you summon.

Input

Input data contains multiple test cases. The first line of input contains integer t — the number of testcases (1≤t≤5).

For each test case,the first line contains two integers n,m,the number of vertices in the graph, the initial number of edges in the graph.

Then m lines follow, each line contains three integers x, y and w x, y and w (0≤x,y<n,−109-109≤w≤109, x!=y)denoting an edge from vertices x to y (0-indexed) of weight w.

Then 6 lines follow, each line contains two integers s,t denoting the starting vertex and the ending vertex of the edge you need to add to the graph.

It is guaranteed that there is not an edge starting from s to t before you add any edges and there must exists such an edge which has the lowest weight and satisfies the above restrictions, meaning the solution absolutely exists for each query.

Output

For each test case,output 6 lines.

Each line contains the weight of the edge you add to the graph.

样例输入

1
10 15
4 7 10
7 6 3
5 3 3
1 4 11
0 6 20
9 8 25
3 0 9
1 2 15
9 0 27
5 2 0
7 3 -5
1 7 21
5 0 1
9 3 16
1 8 4
4 1
0 3
6 9
2 1
8 7
0 4

样例输出

-11
-9
-45
-15
17
7

使用链式前向星存图+SPFA寻找单源最短路径
一开始没有读懂题,画了样例的图感觉是最短路的负数,直接试了一下SPFA模板AC了
记得每次询问之后要把询问的边添加到图中

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int const INF = 0x3f3f3f3f;
int const maxn = 1005;

int dis[maxn];
int head[maxn];
int n, m, cnt;
bool vis[maxn];

struct NODE
{
    int v, w, next;
} edge[100005];

void AddEdge(int u, int v, int w)
{
    edge[cnt].v = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

void SPFA(int s)
{
    memset(dis, 0x3f, sizeof dis);
    memset(vis, 0, sizeof vis);
    dis[s] = 0;

    queue<int>que;
    que.push(s);
    while(que.size())
    {
        int u = que.front();
        que.pop();
        vis[u] = 0;

        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].v;
            int w = edge[i].w;
            if(dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                if(!vis[v])
                {
                    vis[v] = 1;
                    que.push(v);
                }
            }
        }
    }
}

int main()
{
    int T, u, v, w;

    scanf("%d", &T);
    while(T--)
    {
        cnt = 0;
        memset(head, -1, sizeof(head));

        scanf("%d %d", &n, &m);
        while(m--)//建图
        {
            scanf("%d %d %d", &u, &v, &w);
            AddEdge(u, v, w);
        }

        for(int i = 0; i < 6; i++)
        {
            scanf("%d%d", &u, &v);
            SPFA(v);
            AddEdge(u, v, -dis[u]);//每次查询完要将边添加到图中
            printf("%d\n", -dis[u]);//输出最短路径的负值
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪猪侠的Laser_Cannon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值