POJ 2240 Bellman算法判正權迴路 floyd算法

又WA了好多次,主要錯誤是map沒有clear,判重時直接map.count()了,少一個==0,對每一個點都要做bellman()直到找到正權迴路。

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<sstream>
#define ll long long
using namespace std;
const int N_MAX = 40;
const int INF = 0x00ffffff;
const double M_DBL_MAX = 1.7976931348623158e+308;
const double M_DBL_MIN = 2.2250738585072014e-308;
struct edge{
  int l,r;
  double rate;
  edge (int x,int y,double z){l=x,r=y,rate=z;}
  edge (){}
  edge& operator = (const edge& src){
	l=src.l;r=src.r;rate=src.rate;
	return *this;
  }
};
/**********************************************************/
int n,m;
map<string,int> str2ID;
vector<int> curID;
double myMap[N_MAX][N_MAX];
edge myedge[N_MAX*N_MAX];
double dist[N_MAX];
/**********************************************************/
int min_2 (int x,int y) {return x<y?x:y;}
int max_2 (int x,int y) {return x>y?x:y;}
void swap (int& a, int& b){a^=b;b^=a;a^=b;}
bool bellman_ford (int i);
/**********************************************************/
int main()
{
  //freopen ("in.txt","r",stdin);
  int t=1;
  while (cin>>n&&n)
  {
	string curName1,curName2;
	int curID=0;
	str2ID.clear ();
	for (int i=0;i<n;i++){
	  cin>>curName1;
	  if ( str2ID.count(curName1)==0 )
		str2ID[curName1]=curID++;
	}
	cin>>m;
	double rate;
	for (int i=0;i<m;i++){
	  cin>>curName1>>rate>>curName2;
	  myedge[i]=edge (str2ID[curName1],str2ID[curName2],rate);
	  //myMap[ str2ID[curName1] ][ str2ID[curName2] ]=rate;
	}
	bool sucs=false;
	for (int i=0;i<n;i++)
	  if (bellman_ford (i)){
		sucs=true;
		break;
	  } 
	if (sucs)
		cout<<"Case "<<t++<<": Yes"<<endl;
	else
	  cout<<"Case "<<t++<<": No"<<endl;
  }
  return 0;
}
bool bellman_ford (int z)
{
  memset (dist,0,sizeof (dist));
  dist[z]=1.0;
  for (int k=0;k<n-1;k++){
	bool flag=false;
	for (int i=0;i<m;i++){
	  int x=myedge[i].l, y=myedge[i].r;
	  if (dist[y]<dist[x]*myedge[i].rate){
		dist[y]=dist[x]*myedge[i].rate;
		flag=true;
	  }
	}
	if (!flag) break;
  }
  for (int i=0;i<m;i++)
	if (dist[myedge[i].r]<dist[myedge[i].l]*myedge[i].rate)
	  return true;
  return false;
}
在上面beillman函數中可以作這樣做,最外層循環n次,這樣就可以保證將dist[z]更新,然後判斷dist[z]與1.0的大小,來判斷點z在不在正權迴路上。

bool bellman_ford (int z)
{
  memset (dist,0,sizeof (dist));
  dist[z]=1.0;
  for (int k=0;k<n;k++){
    bool flag=false;
    for (int i=0;i<m;i++){
      int x=myedge[i].l, y=myedge[i].r;
      if (dist[y]<dist[x]*myedge[i].rate){
        dist[y]=dist[x]*myedge[i].rate;
        flag=true;
      }
    }
    if (!flag) break;
  }
  return dist[z]>1.0;
}

這道題也可以使用能用floyd算法求每兩個點之間的匯率,即有向邊的權值,然後將兩個點之間的兩條有向邊的權值相乘(沒有邊權值爲零),如果大於1就可以了。但是要注意有aaa 10 aaa這種情況。

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<sstream>
#define ll long long
using namespace std;
const int N_MAX = 40;
const int INF = 0x00ffffff;
const double M_DBL_MAX = 1.7976931348623158e+308;
const double M_DBL_MIN = 2.2250738585072014e-308;
struct edge{
  int l,r;
  double rate;
  edge (int x,int y,double z){l=x,r=y,rate=z;}
  edge (){}
  edge& operator = (const edge& src){
	l=src.l;r=src.r;rate=src.rate;
	return *this;
  }
};
/**********************************************************/
int n,m;
map<string,int> str2ID;
vector<int> curID;
double myMap[N_MAX][N_MAX];
edge myedge[N_MAX*N_MAX];
double dist[N_MAX];
/**********************************************************/
int min_2 (int x,int y) {return x<y?x:y;}
int max_2 (int x,int y) {return x>y?x:y;}
void swap (int& a, int& b){a^=b;b^=a;a^=b;}
bool bellman_ford (int i);
bool floyd ();
/**********************************************************/
int main()
{
  //freopen ("in.txt","r",stdin);
  int t=1;
  while (cin>>n&&n)
  {
	string curName1,curName2;
	int curID=0;
	str2ID.clear ();
	for (int i=0;i<n;i++){
	  cin>>curName1;
	  if ( str2ID.count(curName1)==0 )
		str2ID[curName1]=curID++;
	}
	cin>>m;
	double rate;
	memset (myMap,0,sizeof (myMap));
	for (int i=0;i<m;i++){
	  cin>>curName1>>rate>>curName2;
	  myedge[i]=edge (str2ID[curName1],str2ID[curName2],rate);
	  myMap[ str2ID[curName1] ][ str2ID[curName2] ]=rate;
	}
	if (floyd ())
		cout<<"Case "<<t++<<": Yes"<<endl;
	else
	  cout<<"Case "<<t++<<": No"<<endl;
  }
  return 0;
}
bool floyd ()
{
  for (int k=0;k<n;k++)
	for (int i=0;i<n;i++)
	  for (int j=0;j<n;j++)
		if (i!=j && myMap[i][j]<myMap[i][k]*myMap[k][j])
		  myMap[i][j]=myMap[i][k]*myMap[k][j];
  for (int i=0;i<n;i++)
	for (int j=0;j<n;j++)
	  if (myMap[i][j]*myMap[j][i]>1.0)
		return true;
  return false;
}



發佈了90 篇原創文章 · 獲贊 7 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章