ZOJ1260 POJ1364 HDU1531 King

2298 단어
차분 제약 판단 마이너스 고리
벨맨 템플릿을 찾아서...
/*******************************************************************************
 # Author : Neo Fung
 # Email : [email protected]
 # Last modified: 2012-04-24 20:35
 # Filename: ZOJ1260 POJ1364 HDU1531 King.cpp
 # Description : 
 ******************************************************************************/
#ifdef _MSC_VER
#define DEBUG
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <functional>
#include <ctype.h>
#define MAX 110
using namespace std;

//  ,
typedef struct Edge{
  int u, v;    //  , 
  int weight;  //  
}Edge;

Edge edge[MAX];     //  
int  dist[MAX];     //  

int nodenum, edgenum, source;    //  , , 


//  
void relax(int u, int v, int weight)
{
  if(dist[v] < dist[u] + weight)
    dist[v] = dist[u] + weight;// source v source u v , 
}

bool Bellman_Ford()
{
  for(int i=0; i<=nodenum; ++i)
    for(int j=0; j<edgenum; ++j)
      relax(edge[j].u, edge[j].v, edge[j].weight);
  bool flag = 1;
  //  ,1 
  for(int i=0; i<edgenum; ++i)
    if(dist[edge[i].v] < dist[edge[i].u] + edge[i].weight)
    {
      flag = 0;
      break;
    }
    return flag;
}

int main(void)
{
#ifdef DEBUG  
  freopen("../stdin.txt","r",stdin);
  freopen("../stdout.txt","w",stdout); 
#endif  

//   int n,ncase=1;
  int u,v,d;
  char str[5];
  //   scanf("%d",&ncase);

  while(~scanf("%d",&nodenum) && nodenum)
  {
    scanf("%d",&edgenum);
    for(int i=0;i<edgenum;++i)
    {
      scanf("%d %d %s %d",&u,&v,str,&d);
      if(str[0]=='g')
      {
        edge[i].u=u;
        edge[i].v=u+v+1;
        edge[i].weight=d+1;
      }
      else
      {
        edge[i].u=u+v+1;
        edge[i].v=u;
        edge[i].weight=1-d;
      }
    }
    for(int i=0;i<=nodenum;++i)
      dist[i]=-100000000;

    if(Bellman_Ford())
      printf("lamentable kingdom
"); else printf("successful conspiracy
"); } return 0; }

좋은 웹페이지 즐겨찾기