关闭

soj 4526 不重叠最短路条数

147人阅读 评论(0) 收藏 举报
分类:

分析:记录路径,每一次最短路都删去边,最后最短路变长了就停止。

/**********************jibancanyang**************************
 *Author*        :jibancanyang
 *Created Time*  : 一  5/30 17:18:48 2016
**Problem**:
**Analyse**:
**Get**:
**Code**:
***********************1599664856@qq.com**********************/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
vector<int> vi;
#define pr(x) cout << #x << ": " << x << "  " 
#define pl(x) cout << #x << ": " << x << endl;
#define pri(a) printf("%d\n",(a));
#define xx first
#define yy second
#define sa(n) scanf("%d", &(n))
#define sal(n) scanf("%lld", &(n))
#define sai(n) scanf("%I64d", &(n))
#define vep(c) for(decltype((c).begin() ) it = (c).begin(); it != (c).end(); it++) 
const int mod = int(1e9) + 7, INF = 0x3f3f3f3f;
const int maxn = 1e3 + 13;

struct node {
    int to, cost;
    node (int A, int B): to(A), cost(B) {}
};
int vis[maxn][maxn];
int logs[maxn][maxn];
vector<node> G[maxn];
int d[maxn], pre[maxn],s, e, n, m;

int dijkstra(void) {
    fill(d + 1, d + n + 1, INF);
    priority_queue<pii, vector<pii>, greater<pii> > que;
    que.push(pii(0, s));
    d[s] = 0;
    pre[s] = -1;
    while (!que.empty()) {
        pii cur = que.top(); que.pop();
        if (cur.yy == e) return cur.xx; 
        if (cur.xx > d[cur.yy]) continue;
        for (int i = 0; i < (int)G[cur.yy].size(); i++) {
            node nxt = G[cur.yy][i];
            if (vis[cur.yy][nxt.to] <= 0) continue;
            if (d[nxt.to] > cur.xx + nxt.cost) {
                d[nxt.to] = cur.xx + nxt.cost;
                que.push(pii(d[nxt.to], nxt.to));
                pre[nxt.to] = cur.yy;
            }
        }
    }
    return -1;
}

int main(void)
{
#ifdef LOCAL
    freopen("/Users/zhaoyang/in.txt", "r", stdin);
    //freopen("/Users/zhaoyang/out.txt", "w", stdout);
#endif
    int T; sa(T);
    while (T--) {
        memset(vis, 0, sizeof(vis));
        sa(n), sa(m);
        for (int i = 1; i <= n; i++) fill(logs[i] + 1, logs[i] + n + 1, INF);
        for (int i = 0; i < m; i++) {
            int x, y, z;
            sa(x), sa(y), sa(z);
            if (z < logs[x][y]) {vis[x][y] = 1; logs[x][y] = z;}
            else if (z == logs[x][y]) vis[x][y]++;
            else continue;
            G[x].push_back(node(y, z));
        }
        sa(s), sa(e);
        int mins = dijkstra(), ans = 0;
        if (mins == -1) puts("0");
        else {
            while (true) {
                ans++;
                int x = e;
                while (pre[x] != -1) {
                //    pr(pre[x]), pl(x);
                    vis[pre[x]][x]--;
                    x = pre[x];
                }
                x = dijkstra();
                if (x != mins) break;
            }
            pri(ans);
        }
    }
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:142139次
    • 积分:5751
    • 等级:
    • 排名:第4656名
    • 原创:446篇
    • 转载:3篇
    • 译文:0篇
    • 评论:37条