zoukankan      html  css  js  c++  java
  • POJ

    POJ - 3268 题目链接
    一道最短路问题,但是和单源对短路又不一样,这里有来回的问题
    首先每头牛回的路径很简单,就是以终点为起始点来通过一个dis数组来记录他们回到原粗的距离,
    因为这里是单向边,每头牛到终点的距离肯定和回到起始点的距离不一样,
    这里我们考虑反向建边,又再一次把问题变成的从终点往回走的问题了,
    这样我们统计两个dis数组的值就行了。

    //Powered by CK
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    typedef pair<int, int> PII;
    const int INF = 0x3f3f3f3f;
    const int N1 = 1e3 + 10, N2 = 1e5 + 10;
    int head1[N1], to1[N2], nex1[N2], value1[N2], visit1[N1], dis1[N1], cnt1 = 1;
    int head2[N1], to2[N2], nex2[N2], value2[N2], visit2[N1], dis2[N2], cnt2 = 1;
    int n, m, s;
    struct cmp {
        bool operator () (const PII & a, const PII & b) const {
            return a.second > b.second;
        }
    };
    void add1(int x, int y, int w) {
        to1[cnt1] = y;
        value1[cnt1]  = w;
        nex1[cnt1] = head1[x];
        head1[x] = cnt1++;
    }
    void add2(int x, int y, int w) {
        to2[cnt2] = y;
        value2[cnt2] = w;
        nex2[cnt2] = head2[x];
        head2[x] = cnt2++;
    }
    void Dijkstra1() {
        priority_queue<PII, vector<PII>, cmp> q;
        for(int i = 1; i <= n; i++) dis1[i] = INF;
        dis1[s] = 0;
        q.push(make_pair(s, 0));
        while(!q.empty()) {
            int temp = q.top().first;
            q.pop();
            if(visit1[temp])    continue;
            visit1[temp] = 1;
            for(int i = head1[temp]; i; i = nex1[i])
                if(dis1[to1[i]] > dis1[temp] + value1[i]) {
                    dis1[to1[i]] = dis1[temp] + value1[i];
                    q.push(make_pair(to1[i], dis1[to1[i]]));
                }
        }
    }
    void Dijkstra2() {
        priority_queue<PII, vector<PII>, cmp> q;
        for(int i = 1; i <= n; i++) dis2[i] = INF;
        dis2[s] = 0;
        q.push(make_pair(s, 0));
        while(!q.empty()) {
            int temp = q.top().first;
            q.pop();
            if(visit2[temp])    continue;
            visit2[temp] = 1;
            for(int i = head2[temp]; i; i = nex2[i])
                if(dis2[to2[i]] > dis2[temp] + value2[i]) {
                    dis2[to2[i]] = dis2[temp] + value2[i];
                    q.push(make_pair(to2[i], dis2[to2[i]]));
                }
        }
    }
    int main() {
        // freopen("in.txt", "r", stdin);
        int x, y, w;
        while(scanf("%d %d %d", &n, &m, &s) != EOF) {
            memset(visit1, 0, sizeof visit1);
            memset(visit2, 0, sizeof visit2);
            memset(head1, 0, sizeof head1);
            memset(head2, 0, sizeof head2);
            cnt1 = cnt2 = 1;
            for(int i = 0; i < m; i++) {
                scanf("%d %d %d", &x, &y, &w);
                add1(x, y, w);
                add2(y, x, w);
            }
            Dijkstra1();
            Dijkstra2();
            int ans = 0;
            for(int i = 1; i <= n; i++)
                ans = max(ans, dis1[i] + dis2[i]);
            printf("%d
    ", ans);
        }
        return 0;
    }
    
    
  • 相关阅读:
    *关于如何获取指定表的指定字段的数据类型的方法
    *检查表是否存在
    *用Sql添加删除字段,判断字段是否存在的方法
    *MSSQL三个关键系统表
    *系统表的应用
    VC++ ADO编程入门简介
    int i=0; i=i++ System.out.Println(i)
    Microsoft Visual C++ 6.0 关联源代码
    VC ADO接口详解
    程序员每年该做的事
  • 原文地址:https://www.cnblogs.com/lifehappy/p/12608244.html
Copyright © 2011-2022 走看看