IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> Invitation Cards(建反图 + 跑两遍SPFA) -> 正文阅读

[数据结构与算法]Invitation Cards(建反图 + 跑两遍SPFA)

题目如下:

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.

The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

Output

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

Sample

InputOutput
2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50
46
210

思路:

题目想要的是:

1点到各个点的最短路之和

再加上

各个点到1点的最短路之和

所有我们可以 建反图 + 跑两边SPFA就可以解决了!

AC代码如下:(ps:关闭输入输出流也可能会T,建议直接C输入输出写法)

#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#define ll long long
#define buff                     \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0)
#define endl "\n"
using namespace std;
const int N = 1000000 + 9;
int n, m;
int h[N], ne[N], e[N], idx;
int h2[N], ne2[N], e2[N], idx2;
ll w[N], w2[N];
int dist[N], dist2[N];
bool st[N];
void add(int a, int b, int c)
{
    e[idx] = b;
    w[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}
void add2(int a, int b, int c)
{
    e2[idx2] = b;
    w2[idx2] = c;
    ne2[idx2] = h2[a];
    h2[a] = idx2++;
}
void spfa()
{
    for (int i = 1; i <= n; i++)
    {
        dist[i] = 0x3f3f3f3f;
        st[i] = 0;
    }
    queue<int> q;
    q.push(1);
    dist[1] = 0;
    st[1] = 1;
    while (!q.empty())
    {
        int t = q.front();
        q.pop();
        st[t] = 0;
        for (int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                if (!st[j])
                {
                    st[j] = 1;
                    q.push(j);
                }
            }
        }
    }
}
void spfa2()
{
    for (int i = 1; i <= n; i++)
    {
        st[i] = 0;
        dist2[i] = 0x3f3f3f3f;
    }
    queue<int> q;
    q.push(1);
    dist2[1] = 0;
    st[1] = 1;
    while (!q.empty())
    {
        int t = q.front();
        q.pop();
        st[t] = 0;
        for (int i = h2[t]; i != -1; i = ne2[i])
        {
            int j = e2[i];
            if (dist2[j] > dist2[t] + w2[i])
            {
                dist2[j] = dist2[t] + w2[i];
                if (!st[j])
                {
                    st[j] = 1;
                    q.push(j);
                }
            }
        }
    }
}
void init()
{
    idx = 0;
    idx2 = 0;
    memset(h, -1, sizeof h);
    memset(h2, -1, sizeof h2);
}
void solve()
{
    cin >> n >> m;
    init();
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        ll c;
        //cin >> a >> b >> c;
        scanf("%d %d %lld",&a,&b,&c);
        add(a, b, c);
        add2(b, a, c);
    }
    spfa();
    spfa2();
    ll ans = 0;
    for (int i = 1; i <= n; i++)
    {
        ans += dist[i] + dist2[i];
    }
    printf("%lld\n",ans);
}
signed main()
{
    //buff;
    int t;
    cin >> t;
    while (t--)
        solve();
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2022-04-15 00:24:47  更:2022-04-15 00:25:09 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 7:30:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码