UVa 515 King (差分約束系統)

來源:互聯網
上載者:User

/* **********************************************Author      : JayYeCreated Time: 2013/10/7 15:26:16File Name   : Orz.cpp*********************************************** */#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int maxn = 100 + 5;const int INF = 1<<30;struct Edge{    int u, to, next, val;}edge[maxn*maxn];int head[maxn], d[maxn], vis[maxn], T[maxn], q[maxn*maxn], n, E;void init() {    memset(head, -1, sizeof(head));    E = 0;}void newedge(int u, int to, int val) {    edge[E].u = u;    edge[E].to = to;    edge[E].val = val;    edge[E].next = head[u];    head[u] = E++;}bool SPFA() {    for(int i = 0;i <= n + 1; i++)  {        d[i] = INF; vis[i] = T[i] = 0;    }    T[n+1] = 1;    d[n+1] = 0;    vis[n+1] = 1;    int st = 0, ed = 0;    q[ed++] = n+1;    while(st < ed) {        int u = q[st++];        vis[u] = 0;        for(int i = head[u];i != -1;i = edge[i].next) {            int to = edge[i].to, val = edge[i].val;            if(d[to] > d[u] + val) {                d[to] = d[u] + val;                if(!vis[to]) {                    vis[to] = 1;                    T[to]++;                    if(T[to] > n + 1)   return true;                    q[ed++] = to;                }            }        }    }    return false;}int main() {    int m;    while(scanf("%d", &n) != -1 && n) {        init();        scanf("%d", &m);        for(int i = 0;i < m; i++) {            int fr, len, k;            char op[4];            scanf("%d%d%s%d", &fr, &len, op, &k);            int u = fr + len, to = fr-1;            if(op[0] == 'g')                newedge(u, to, -1 - k);            else                newedge(to, u, k - 1);        }        for(int i = 0;i <= n; i++)  newedge(n+1, i, 0);        printf("%s\n", SPFA() ? "successful conspiracy" : "lamentable kingdom");    }    return 0;}

Bellman code:

/* **********************************************Author      : JayYeCreated Time: 2013/10/7 15:26:16File Name   : Orz.cpp*********************************************** */#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int maxn = 100 + 5;struct Edge{    int u, to, next, val;}edge[maxn*maxn*2];int head[maxn], d[maxn], n, E;void init() {    memset(head, -1, sizeof(head));    E = 0;}void newedge(int u, int to, int val) {    edge[E].u = u;    edge[E].to = to;    edge[E].val = val;    edge[E].next = head[u];    head[u] = E++;}bool Bellman() {    memset(d, 0, sizeof(d));    for(int i = 0;i < n+1; i++) {        for(int j = 0;j < E; j++) {            int u = edge[j].u, to = edge[j].to, val = edge[j].val;            if(d[to] > d[u] + val) {                d[to] = d[u] + val;                if(i == n)  return true;            }        }    }    return false;}int main() {    int m;    while(scanf("%d", &n) != -1 && n) {        init();        scanf("%d", &m);        for(int i = 0;i < m; i++) {            int fr, len, k;            char op[4];            scanf("%d%d%s%d", &fr, &len, op, &k);            int u = fr + len, to = fr-1;            if(op[0] == 'g')                newedge(u, to, - 1 - k);            else                newedge(to, u, k - 1);        }        printf("%s\n", Bellman() ? "successful conspiracy" : "lamentable kingdom");    }    return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.