北大2018acm暑期课六最短路&&最小生成树

Candies

描述

During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.

snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?

输入

The input contains a single test cases. The test cases starts with a line with two integers Nand M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers AB and c in order, meaning that kid Abelieved that kid B should never get over c candies more than he did.

输出

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

样例输入

2 2
1 2 5
2 1 4

样例输出

5

提示

32-bit signed integer type is capable of doing all arithmetic.

来源

POJ Monthly--2006.12.31, Sempr

老师讲的大概是要满足所有的条件就要满足最苛刻的条件这种意思,百度下来叫做差分约束emmmmmmm反正就是求最短路。

然后写dijkstraTE了,打开课件学习了一下spfa,queue版本的又TE了,写了个首尾指针的RE了,开10^7都RE了,然后百度,是spfa+stack,原来最短路还能spfa加stack,大喊666。spfa加queue是宽搜着松弛,spfa加stack是深搜松弛,我觉得queue应该更快啊怎么是stack更快呢。

#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 30010
#define M 150010
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = acos(-1);
#define fi first
#define se second
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

int n, m;
int d[N];
bool vis[N];
struct Edge {
  int v, w, nex;
}edge[M];
int tot, fir[M], q[N];
void init()
{
    tot = 0;
    rep(i, 0, m) fir[i] = -1;
}
void addedge(int u, int v, int w)
{
    edge[tot].v = v;
    edge[tot].w = w;
    edge[tot].nex = fir[u];
    fir[u] = tot++;
}
void spfa()
{
    memset(vis, false, sizeof vis);
    memset(d, INF, sizeof d);
    int v, l = 0;
    q[l++] = 1;
    d[1] = 0;
    vis[1] = true;

    while(l) {
        v = q[--l];
        vis[v] = false;
        for(int i = fir[v]; i != -1; i = edge[i].nex) {
            if(d[edge[i].v] > d[v] + edge[i].w){
                d[edge[i].v] = d[v] + edge[i].w;
                if(!vis[edge[i].v]) {
                    vis[edge[i].v] = true;
                    q[l++] = edge[i].v;
                }
            }
        }
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif

    scanf("%d%d", &n, &m);

    int a, b, c;
    init();
    Edge e;
    rep(i, 1, m) {
        scanf("%d%d%d", &a, &b, &c);
        addedge(a, b, c);
    }
    spfa();
    printf("%d\n", d[n]);

    return 0;
}

//int n, m;
//struct edge {
//  int v, w;
//};
//vector<edge> G[N];
//int d[N];
//
//void init()
//{
//    rep(i, 0, n) G[i].clear();
//}
//
//void dijkstra()
//{
//    priority_queue<P, vector<P>, greater<P> > que;
//    memset(d, INF, sizeof d);
//    d[1] = 0;
//    que.push(P(0, 1));
//
//    while(!que.empty()) {
//        P h = que.top(); que.pop();
//
//        int v = h.se;
//        if(d[v] < h.fi) continue;
//
//        for(int i = 0; i < G[v].size(); i++) {
//            edge & e = G[v][i];
//            if(d[e.v] > d[v] + e.w) {
//                d[e.v] = d[v] + e.w;
//                que.push(P(e.w, e.v));
//            }
//        }
//    }
//}
//
//int main()
//{
//    #ifndef ONLINE_JUDGE
//    freopen("data.txt", "r", stdin);
//    #endif
//
//    scanf("%d%d", &n, &m);
//
//    int a, b, c;
//    init();
//    edge e;
//    rep(i, 1, m) {
//        scanf("%d%d%d", &a, &b, &c);
//        e.v = b; e.w = c;
//        G[a].push_back(e);
//    }
//    dijkstra();
//    printf("%d\n", d[n]);
//
//    return 0;
//}

Til the Cows Come Home

描述

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.

输入

* Line 1: Two integers: T and N

* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.

输出

* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

样例输入

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

样例输出

90

提示

INPUT DETAILS:

There are five landmarks.

OUTPUT DETAILS:

Bessie can get home by following trails 4, 3, 2, and 1.

 

裸的最短路。

#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 11000
#define M 3645
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = acos(-1);
#define fi first
#define se second
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

int t, n;
struct edge{
  int v, w;
};
vector<edge> G[N];
int d[N];

void dijkstra()
{
    priority_queue< P, vector<P>, greater<P> > que;
    memset(d, INF, sizeof d);
    d[n] = 0;
    que.push(P(0, n));

    while(!que.empty()) {
        P h = que.top(); que.pop();

        int v = h.se;
        if(d[v] < h.fi) continue;

        for(int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if(d[e.v] > d[v] + e.w)
                d[e.v] = d[v] + e.w, que.push(P(e.w, e.v));
        }
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif

    scanf("%d%d", &t, &n);

    int u, v, w;
    edge e;
    rep(i, 1, t) {
        scanf("%d%d%d", &u, &v, &w);
        e.v = v; e.w = w;
        G[u].push_back(e);
        e.v = u;
        G[v].push_back(e);
    }

    dijkstra();

    printf("%d\n", d[1]);

    return 0;
}

Agri-Net

描述

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. 
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. 
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. 
The distance between any two farms will not exceed 100,000. 

输入

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

输出

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

样例输入

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

样例输出

28

裸的最小生成树,我看是稠密图所以用的Prim。

#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 110
#define M 150010
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = acos(-1);
#define fi first
#define se second
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

int n, g[N][N];
int low[N];
bool vis[N];

int prim()
{
    memset(vis, false, sizeof vis);
    rep(i, 1, n) low[i] = g[1][i];
    int ans = 0;
    vis[1] = true;
    rep(i, 2, n) {
        int minc = INF, p;
        rep(j, 1, n) if(!vis[j] && minc > low[j]){
            minc = low[j]; p = j;
        }
       // if(minc == INF) return -1;
        ans += minc;
        vis[p] = true;
        rep(j, 1, n) if(!vis[j] && low[j] > g[p][j])
            low[j] = g[p][j];
    }
    return ans;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif

    while(~scanf("%d", &n) && n) {
        rep(i, 1, n) rep(j, 1, n) scanf("%d", &g[i][j]);
        printf("%d\n", prim());
    }

    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值