bellmanford algorithm
来源:互联网 发布:网络闹钟在线 编辑:程序博客网 时间:2024/06/03 15:07
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
using namespace std;
typedef struct archnode{
int to;
int cost;
struct archnode * next;
}archnode;
typedef struct vnode {
int key;
int cost;
struct archnode *head;
}vnode;
typedef struct graph{
int nodenum;
int archnum;
vnode ** root;
}graph;
void DF(vnode ** root,vnode *start,int color[]){
vnode *begin=start;
archnode * arch=begin->head;
cout<<"key is:"<<begin->key<<" "<<"value is:"<<begin->cost<<endl;
color[begin->key]=1;
while(arch!=NULL){
cout<<"hhah"<<endl;
archnode * next=arch->next;
if(color[arch->to]==1)
{
cout<<"has loop"<<endl;
return;
}
if(color[arch->to]==0){
DF(root,root[arch->to],color);
}
arch=next;
}
return;
}
void DF2(vnode ** root,vnode * start,int color[],int go[],int finish[],int time){
if(color[start->key]!=0)
return;
cout<<"time is"<<endl;
vnode *begin=start;
archnode * arch=begin->head;
cout<<"key is:"<<begin->key<<" "<<"value is:"<<begin->cost<<endl;
color[begin->key]=1;
go[begin->key]=time++;
while(arch!=NULL){
cout<<"haha"<<endl;
archnode * next=arch->next;
if(color[arch->to]==2){
if(go[begin->key] > finish[arch->to])
{
cout<<"not strongly connected componet"<<endl;
return;
}
}
if(color[arch->to]==0)
DF2(root,root[arch->to],color,go,finish,time);
arch=next;
}
color[begin->key]=2;
finish[begin->key]=time++;
}
int main(){
int nodenum,archnum;
scanf("%d %d",&nodenum,&archnum);
graph * G=(graph*) malloc(sizeof(graph));
G->nodenum=nodenum;
G->archnum=archnum;
G->root =(vnode **)malloc(sizeof(vnode *)*(nodenum+1));
vnode ** root=G->root;
for(int index=0;index<nodenum;index++){
root[index+1]=(vnode*)malloc(sizeof(vnode));
root[index+1]->key=index+1;
root[index+1]->cost=INT_MAX;
root[index+1]->head=NULL;
}
for(int index=1;index<=archnum;index++){
int from,to,cost;
scanf("%d %d %d",&from,&to,&cost);
//1
vnode *nodeone=root[from];
archnode *t1=nodeone->head;
archnode * newone=(archnode *)malloc(sizeof(archnode));
newone->to=to;
newone->cost=cost;
newone->next=NULL;
nodeone->head=newone;
newone->next=t1;
//2
// vnode * nodetwo=root[to];
// archnode *t2=nodetwo->head;
// archnode * newtwo=(archnode*)malloc(sizeof(archnode));
// newtwo->to=from;
// newtwo->cost=cost;
// newtwo->next=NULL;
// nodetwo->head=newtwo;
// newtwo->next=t2;
cout<<"index"<<endl;
}
int color[nodenum+1];
int go[nodenum+1];
int finish[nodenum+1];
for(int index=1;index<=nodenum;index++){
color[index]=0;
// go[index]=0;
// finish[index]=0;
}
DF(G->root,root[1],color);
cout<<"test2"<<endl;
int time=1;
for(int index=1;index<=nodenum;index++){
color[index]=0;
go[index]=0;
finish[index]=0;
}
for(int index=1;index<=nodenum;index++){
cout<<"index "<<index<<endl;
DF2(G->root,root[index],color,go,finish,time);
}
for(int index=1;index<=nodenum;index++){
vnode * node=root[index];
archnode * arch=node->head;
while(arch!=NULL){
archnode * next=arch->next;
free(arch);
arch=next;
}
free(root[index]);
cout<<"delete"<<endl;
}
system("pause");
return 0;
}
- bellmanford algorithm
- Bellmanford
- BellmanFord
- BellmanFord 算法
- NYOJ188_Arbitrage(BellmanFord)
- Algorithm
- Algorithm
- algorithm
- algorithm
- algorithm
- algorithm
- algorithm
- Algorithm
- Algorithm
- algorithm
- Algorithm
- Algorithm
- algorithm
- 掌控移动入口:互联网手机的伪命题?
- ibatis 过滤重复数据
- ArcGIS Engine10.0轻松入门级教程(3)——ArcEngine10.0查询分析功能
- 嵌入式系统vxworks
- Ubuntu开启ssh服务
- bellmanford algorithm
- js原型分析研究
- 内存异常系列之Android解决加载大图片时内存溢出的问题
- 内存异常系列之单例模式导致内存泄露
- 关于C/C++左值的一些问题
- MYSQL查询重复记录的方法
- vi编辑器命令详解
- Android,谁动了我的内存
- uestc 1367#先暴力水一个再说