Time Limit: 1000 MS Memory Limit: 131072 KB
Description
In the future ,One day, tom feel so happy ,because he have a date with a girl,but they don’t live in the same city , so tom want you help him find the fastest way to the girl’s city,You should note that with the development of technology, transport can go beyond the speed of light, so the time you spend would be less than zero, but if you return to the past you can not have the date with the girl,there n(1<n<=100) city in this country and three are m(1<m<1000) roads in this country;
Tom in the first city,the girl in the city n;
Input
The fist line is m,n;
Next m lines is a,b,c (a,b is the name of city , c is the time you cost from city a to city b)
Output
The shortest time to reach the girl’s city
(if tom return to the past ,out IMPOSSIBLE!)
Sample Input
1 2
1 2 10
3 4
1 2 10
2 3 10
3 4 -5
Sample Output
10
IMPOSSIBLE!
Bellman-Ford算法是一种最短路算法,适合单源带负权的有向/无向图,核心就是对所有边进行最多n-1次松弛,如果在进行了n-1轮的松弛后,某些边还能进行松弛,那么说明图中包含负权环。
下面是代码:
等等好像出问题了,下面的代码ac了,但是不是正确的判负环的方法:
#include<iostream>
using namespace std;
const int inf=999999999;
struct edge{
int point1;
int point2;
int value;
}dat[1005];
int dis[105];
int main()
{
int m,n;
while(cin>>m>>n)
{
for(int i=1;i<=n;++i)
{
dis[i]=inf;
}
dis[1]=0;
for(int i=1;i<=m;++i)
{
cin>>dat[i].point1>>dat[i].point2>>dat[i].value;
}
for(int i=m+1;i<=m*2;++i)
{
dat[i].point1=dat[i-m].point2;
dat[i].point2=dat[i-m].point1;
dat[i].value=dat[i-m].value;
}//因为是无向图所以进行这个操作
for(int i=1;i<n;++i)//Bellman-Ford算法的核心,就是对所有边松弛最多n-1次,这是未经任何优化的代码
{
for(int j=1;j<=m*2;++j)
{
//cout<<dat[j].point1<<"->"<<dat[j].point2<<" "<<dat[j].value<<endl;
if(dis[dat[j].point2]>dis[dat[j].point1]+dat[j].value)
dis[dat[j].point2]=dis[dat[j].point1]+dat[j].value;
}
}
if(dis[n]<0) cout<<"IMPOSSIBLE!" <<endl;
else
cout<<dis[n]<<endl;
}
return 0;
}
下面的代码wa了,但是我用的却是正确的判负环的方法= =:
#include<iostream>
using namespace std;
const int inf=999999999;
struct edge{
int point1;
int point2;
int value;
}dat[2005];
int dis[105];//这个数组的含义是选定点到某点的最短距离
int main()
{
int m,n;
while(cin>>m>>n)
{
for(int i=1;i<=n;++i)
{
dis[i]=inf;
}
dis[1]=0;
for(int i=1;i<=m;++i)
{
cin>>dat[i].point1>>dat[i].point2>>dat[i].value;
dat[i+m].point1=dat[i].point2;
dat[i+m].point2=dat[i].point1;
dat[i+m].value=dat[i].value;//因为是无向图所以进行这个操作
}
for(int i=1;i<n;++i)//Bellman-Ford算法的核心,就是对所有边松弛最多n-1次,这是未经任何优化的代码
{
for(int j=1;j<=m*2;++j)
{
//cout<<dat[j].point1<<"->"<<dat[j].point2<<" "<<dat[j].value<<endl;
if(dis[dat[j].point2]>dis[dat[j].point1]+dat[j].value)
dis[dat[j].point2]=dis[dat[j].point1]+dat[j].value;
}
}
bool flag=0;
for(int i=1;i<=m*2;++i)
{
if(dis[dat[i].point2]>dis[dat[i].point1]+dat[i].value)
{
flag=1;
break;
}
}
if(flag) cout<<"IMPOSSIBLE!" <<endl;
else
cout<<dis[n]<<endl;
}
return 0;
}
就很离谱,不知道是不是我将这个算法应用在无向图上的方法不对?
SPFA算法可以a,我用了STL中的list辅助实现图的邻接表储存方式,SPFA算法实现的原理是利用了队列对Bellman-Ford算法进行优化,每次检查通过队首的点能否松弛与它直接相连的点,如果能并且该点不在队列中,就将那个点入队,并将其标记为已入队,每次检查完队首的点后让其出队,直到队伍为空,如果存在负权环,则队伍永远不可能为空,如果检测到某点进入队列的次数大于n次了,说明存在负权环,直接跳出循环就行了。
下面是代码:
#include<iostream>
#include<queue>
#include<list>
using namespace std;
const int inf=999999999;//自定义一个最大值
queue<int> qu;
list<int> vt[105];//存放list的对象数组
list<int>::iterator p;//声明list的迭代器
struct edge{
int point1;
int point2;
int value;
}dat[1005];//表示边的结构体
int dis[105];//某点到其余各点的动态最小距离
bool vis[105];//标记某点是否在队中
int times[105];//统计某点入队的次数
int valuee[105][105];//记录某点到某点的距离
int main()
{
int m,n;
while(cin>>m>>n)
{
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
{
valuee[i][j]=0;
}
}
bool flag=0;
for(int i=1;i<=n;++i)
{
times[i]=0;
}
for(int i=1;i<=n;++i)
{
dis[i]=inf;//表示起点暂时还没有直接相连的点
}
dis[1]=0;//让谁为起点就将他赋值为0
for(int i=1;i<=n;++i)
{
vis[i]=0;
}//上面均为初始化操作
for(int i=1;i<=m;++i)
{
cin>>dat[i].point1>>dat[i].point2>>dat[i].value;//读入数据
vt[dat[i].point1].push_back(dat[i].point2);
vt[dat[i].point2].push_back(dat[i].point1);//利用链表表示出与某点相连的所有点,由于是无向图,反着也要来一遍
valuee[dat[i].point1][dat[i].point2]=dat[i].value;
valuee[dat[i].point2][dat[i].point1]=dat[i].value;//记录点1到点2的距离,由于是无向图,反着也要来一遍
}
qu.push(1);//让起点入队
vis[1]=1;//标记起点入队了
times[1]++;//起点入队次数加一
while(!qu.empty())
{
for(p=vt[qu.front()].begin();p!=vt[qu.front()].end();++p)
{
if(dis[*p]>dis[qu.front()]+valuee[qu.front()][*p])
{
dis[*p]=dis[qu.front()]+valuee[qu.front()][*p];
if(!vis[*p])//如果这一点还没有入队
{
qu.push(*p);//某点入队
times[*p]++;//某点入队的次数加一
if(times[*p]>n)//如果某点入队的次数大于n了,那么就说明图中存在负权环,跳出循环。
{
flag=1;
break;
}
vis[*p]=1;//标记某点已入队
}
}
}
if(flag) break;
vis[qu.front()]=0;//标记队首出队了
qu.pop();//队首出队
}
if(flag) cout<<"IMPOSSIBLE!"<<endl;//存在负权环
else
cout<<dis[n]<<endl;
for(int i=1;i<=n;++i)
{
vt[i].clear();//注意清空链表!!!!!
}
}
return 0;
}
//也可以直接把边的结构体用邻接矩阵代替,链表只是起到了表示出相邻点的作用
1万+


(未优化版还存在问题,SPFA没问题了)&spm=1001.2101.3001.11974&articleId=111998392&d=1&t=3&u=c28dfb5ba48b4ad99e54aed744c5482e)

被折叠的 条评论
为什么被折叠?



