soj 4526 不重叠最短路条数

原创 2016年05月30日 18:10:01

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

/**********************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;
}
版权声明:本文为博主原创文章,转载请注明作者:jibancanyang。

相关文章推荐

1796 社交网络[最短路条数]

时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 2007年NOI全国竞赛 题目描述 Description在社交网络(social network)的...

最短路和次短路条数

问题描述:                   给你一张无向图或有向图,要你求任意两点的最短路条数或次短路条数 算法描述:               1,最短路:          ...

HDU 3191How Many Paths Are There(TOPE排序 求次短路及条数)

How Many Paths Are There Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav...

CDOJ_1147 (最短路条数)

分析:最短路条数问题。求最短路的条数只需要在dijkstra上面加一个数组sumt[]记录就行,sumt[v] 表示从源点 s 出发到 v 的最短路条数,当 dist[v] > dist[u] + d...

hdu 3191 How Many Paths Are There(次短路+条数)

How Many Paths Are There Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav...
  • acm_cxq
  • acm_cxq
  • 2016年05月25日 00:56
  • 220

【PAT】Emergency(最短路条数-SPFA)

【PAT】Emergency(最短路条数-SPFA) As an emergency rescue team leader of a city, you are given a specia...

HDU_3191_How Many Paths Are There(次短路条数)

题意:给你一个有向图,求出次短路长度和条数。

HDU-3191 How Many Paths Are There(有向图次短路条数)

oooccc1 is a Software Engineer who has to ride to the work place every Monday through Friday. For a ...
  • yo_bc
  • yo_bc
  • 2017年07月14日 17:25
  • 79

HDU-1688 Sightseeing 最短路与“次短路”条数

http://acm.hdu.edu.cn/showproblem.php?pid=1688 思路:http://blog.csdn.net/u011742541/article/det...

POJ_3463_Sightseeing(最短路/次短路条数)

题意:给你一个有向图,如果次短路长度刚好比最短路大1,那么输出最短路条数与次短路条数之和,否者直接输出最短路的条数。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:soj 4526 不重叠最短路条数
举报原因:
原因补充:

(最多只允许输入30个字)