栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3169 Layout

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3169 Layout

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <queue>using namespace std;#define INF 0x3f3f3f3f#define V 1005#define E 20001int pnt[E], cost[E], nxt[E];int e, head[V], dist[V];bool vis[V];int cnt[V];int relax(int u, int v, int c){    if (dist[v] > dist[u] + c)    {        dist[v] = dist[u] + c;        return 1;    }    return 0;}inline void addedge(int u, int v, int c){    pnt[e] = v;    cost[e] = c;    nxt[e] = head[u];    head[u] = e++;}int SPFA(int src, int n){    int i;    memset(cnt, 0, sizeof(cnt));    memset(vis, false, sizeof(vis));    for (i = 1; i <= n; ++i)        dist[i] = INF;    dist[src] = 0;    queue<int> Q;    Q.push(src);    vis[src] = true;    ++cnt[src];    while (!Q.empty())    {        int u, v;        u = Q.front();        Q.pop();        vis[u] = false;        for (i = head[u]; i != -1; i = nxt[i])        { v = pnt[i]; if (1 == relax(u, v, cost[i]) && !vis[v]) {     Q.push(v);     vis[v] = true;     if ((++cnt[v]) > n)         return -1; }        }    }    if (dist[n] == INF)        return -2;    return dist[n];}int main(){    int n, ml, md;    int i, a, b, c;    e = 0;    scanf("%d%d%d", &n, &ml, &md);    memset(head, -1, sizeof(head));    for (i = 0; i < ml; ++i)    {        scanf("%d%d%d", &a, &b, &c);        addedge(a, b, c);    }    for (i = 0; i < md; ++i)    {        scanf("%d%d%d", &a, &b, &c);        addedge(b, a, -c);    }    printf("%dn", SPFA(1, n));    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378562.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号