9-13 (图论之手练模板篇,存边小技巧)

一个人的旅行

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女……眼看寒假就快到了,这么一大段时间,可不能浪费啊,一定要给自己好好的放个假,可是也不能荒废了训练啊,所以草儿决定在要在最短的时间去一个自己想去的地方!因为草儿的家在一个小镇上,没有火车经过,所以她只能去邻近的城市坐火车(好可怜啊~)。

Input

输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个;
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。

Output

输出草儿能去某个喜欢的城市的最短时间。

Sample Input

6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10

Sample Output

9

真是很水的最短路题==

手写dij版

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
typedef long long ll;
const int INF = 0x7f7f7f7f;
typedef pair<ll, int> pii;

struct edge{
    int to, time;
};

struct dijstra{
    vector<edge> vec[maxn];
    ll  dist[maxn];

    void init(){
        for(int i = 0; i < maxn; i++) vec[i].clear();
        for(int i = 0; i < maxn; i++) dist[i] = INF;
    }

    void add_edge(int u, int v, int c){
        vec[u].push_back(edge{v, c});
        vec[v].push_back(edge{u, c});
    }

    void dijs(int s){
        dist[s]  = 0;
        priority_queue<pii, vector<pii>, greater<pii> > que;
        que.push(pii{0, s});

        while(!que.empty()){
            pii p = que.top();
            que.pop();
            if(p.first != dist[p.second]) continue;
            int u = p.second;
            for(int i = 0; i < vec[u].size(); i++){
                edge e = vec[u][i];
                if(dist[e.to] > dist[u] + e.time){
                    dist[e.to] = dist[u] + e.time;
                    que.push(pii{dist[e.to], e.to});
                }
            }
        }
    }

}dij;

int t, s, d;

int main()
{
    while(~scanf("%d%d%d", &t, &s, &d)){
        dij.init();
        for(int i = 0; i < t; i++){
            int u, v, tt;
            scanf("%d%d%d", &u, &v, &tt);
            dij.add_edge(u, v, tt);
        }
        for(int i = 0; i < s; i++){
            int s; scanf("%d", &s);
            dij.add_edge(0, s, 0);
        }

        dij.dijs(0);
        ll ans = INF;
        for(int i = 0; i < d; i++){
            int v; scanf("%d", &v);
            ans = min(ans, dij.dist[v]);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

这个用Flord肯定超,但还是写出来

#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
typedef long long ll;
ll dist[maxn][maxn];
const ll INF = 0x7f7f7f7f;

int t, s, d;

void ford(){
    for(int k = 0; k < maxn; k++)
        for(int i = 0; i < maxn; i++)
            for(int j = 0; j < maxn; j++)
                if(dist[i][k] != INF && dist[k][j] != INF)
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}


int main()
{
    while(~scanf("%d%d%d", &t, &s, &d)){
        for(int i = 0; i < maxn; i++)
            for(int j = 0; j < maxn; j++)
                dist[i][j] = INF;
        for(int i = 0; i < t; i++){
            int u, v, t;
            scanf("%d%d%d", &u, &v, &t);
            dist[u][v] = dist[v][u] = t;
        }
        for(int i = 0; i < s; i++){
            int s; scanf("%d", &s);
            dist[0][s] = dist[s][0] = 0;
        }
        ford();
        ll ans = INF;
        for(int i = 0; i < d; i++){
            int v; scanf("%d", &v);
            ans = min(ans, dist[0][v]);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

spfa版

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
typedef long long ll;
const ll INF = 0x7f7f7f7f;
struct edge{
    int to, time;
};
struct Spfa{
    vector<edge> vec[maxn];
    ll dist[maxn];
    int vis[maxn];
    void init(){
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < maxn; i++) vec[i].clear();
        for(int i = 0; i < maxn; i++) dist[i] = INF;
    }
    void add_edge(int u, int v, int c){
        vec[u].push_back(edge{v, c});
        vec[v].push_back(edge{u, c});
    }

    void spfa(int s){
        dist[s] = 0;
        queue<int> que;
        que.push(s);
        vis[s] = 1;
        while(!que.empty()){
            int u = que.front();
            que.pop();
            for(int i = 0; i < vec[u].size(); i++){
                edge e = vec[u][i];
                if(dist[e.to] > dist[u] + e.time){
                    dist[e.to] = dist[u] + e.time;
                    if(!vis[e.to]){
                        vis[e.to] = 1;
                        que.push(e.to);
                    }
                }
            }
        }
    }
}sp;



int main()
{
    int t, s, d;
    while(~scanf("%d%d%d", &t, &s, &d)){
        sp.init();
        for(int i = 0; i < t; i++){
            int u, v, t;
            scanf("%d%d%d", &u, &v, &t);
            sp.add_edge(u, v, t);
        }
        for(int i = 0; i < s; i++){
            int ss; scanf("%d", &ss);
            sp.add_edge(0, ss, 0);
        }
        sp.spfa(0);
        ll ans = INF;
        for(int i = 0; i < d; i++){
            int v; scanf("%d", &v);
            ans = min(ans, sp.dist[v]);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

Drainage Ditches

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 77980 Accepted: 30399

Description

Every time it rains on Farmer John’s fields, a pond forms over Bessie’s favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie’s clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

Edmonds Karp版网络流, 熟悉原理和原因, 实际中可以直接运用茅叔叔ISAP的模板

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath> 
#include <queue>
#include <cstring>
#include <string>
using namespace std;

struct Edge{
    int to, from, cap, flow;
};
typedef long long ll;
const int maxn = 210;
const int INF = 0x3f3f3f3f;

struct Edmonds{
    vector<Edge> edge;
    vector<int> vec[maxn];
    int a[maxn];
    int pre[maxn];

    void init(){
        edge.clear();
        for(int i = 0; i < maxn; i++) vec[i].clear();
    }

    void add_edge(int u, int v, int c){
        edge.push_back(Edge{v, u, c, 0});
        edge.push_back(Edge{u, v, 0, 0});
        vec[u].push_back(edge.size()-2);
        vec[v].push_back(edge.size()-1);
    }

    int max_flow(int s, int t){
        int flow = 0;

        while(1){
            queue<int> que;
            que.push(s);
            memset(a, 0, sizeof(a));
            a[s] = INF;
            while(!que.empty()){
                int u = que.front();
                que.pop();
                for(int i = 0; i < vec[u].size(); i ++){
                    Edge e = edge[vec[u][i]];
                    if(!a[e.to] && e.cap > e.flow){
                        a[e.to] = min(a[u], e.cap - e.flow);
                        pre[e.to] = vec[u][i];
                        que.push(e.to);
                    }
                }
                if(a[t]) break;
            }
            if(!a[t]) return flow;

            for(int i = t; i != s; i = edge[pre[i]].from){
                edge[pre[i]].cap -= a[t];
                edge[pre[i]^1].cap += a[t]; //想一想这里为什么是异或,小技巧
            }
            flow += a[t];
        }

    }
}edmon;

int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m)){
        edmon.init();
        for(int i = 0; i < n; i++){
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            edmon.add_edge(u, v, c);
        }

        int ans = edmon.max_flow(1, m);
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值