POJ 3767 I Wanna Go Home【最短路,Dijkstra+spfa,题意是关键呀】
2016-09-22 21:07
399 查看
I Wanna Go Home
Description
The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him
reach home as soon as possible.
"For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."
Would you please tell Mr. M at least how long will it take to reach his sweet home?
Input
The input contains multiple test cases.
The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
The second line contains one integer M (0<=M<=10000), which is the number of roads.
The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range
of [1,500].
Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.
To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.
Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.
Output
For each test case, output one integer representing the minimum time to reach home.
If it is impossible to reach home according to Mr. M's demands, output -1 instead.
Sample Input
Sample Output
原题链接:http://poj.org/problem?id=3767
题意:一个国家有n个城市,之间共有m条路,每座城市属于group1或group2,1一定属于group1 , 2一定属于group2,属于group2 的城市不能直接到达属于group1的城市,问从1到2的最短路。
有人说建图是关键,但我认为,松弛操作时才是关键。建图双向边单向边不好控制。
如果均建双向边,求解的过程中避免直接从group2到group1即可。
理解题意后DIjkstra算法和spfa算法随便来。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=605;
const int INF=0x3f3f3f3f;
int a[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int fa[maxn];
int n,m;
void Dijkstra()
{
for(int i=1; i<=n; i++)
{
dis[i]=a[1][i];
vis[i]=false;
}
dis[1]=0;
vis[1]=true;
for(int i=0; i<n; i++)
{
int minn=INF;
int p=-1;
for(int j=1; j<=n; j++)
{
if(!vis[j]&&dis[j]<minn)
minn=dis[p=j];
}
if(p==-1)
break;
vis[p]=true;
for(int j=1; j<=n; j++)
{
if(fa[p]==2&&fa[j]==1)
continue;
if(!vis[j]&&dis[j]>dis[p]+a[p][j])
dis[j]=dis[p]+a[p][j];
}
}
}
void spfa()
{
for(int i=1; i<=n; i++)
{
dis[i]=INF;
vis[i]=false;
}
dis[1]=0;
vis[1]=true;
queue<int>q;
q.push(1);
while(!q.empty())
{
int p=q.front();
q.pop();
vis[p]=false;
for(int i=1; i<=n; i++)
{
if(fa[p]==2&&fa[i]==1)
continue;
if(dis[i]>dis[p]+a[p][i])
{
dis[i]=dis[p]+a[p][i];
if(!vis[i])
{
vis[i]=true;
q.push(i);
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("data/3767.txt","r",stdin);
while(cin>>n,n)
{
cin>>m;
for(int i=0; i<=n; i++)
{
for(int j=0; j<=n; j++)
if(i==j) a[i][j]=0;
else a[i][j]=INF;
}
int x,y,z;
while(m--)
{
cin>>x>>y>>z;
a[x][y]=a[y][x]=z;
}
for(int i=1; i<=n; i++)
cin>>fa[i];
//Dijkstra();
spfa();
if(dis[2]<INF)
cout<<dis[2]<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3499 | Accepted: 1504 |
The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him
reach home as soon as possible.
"For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."
Would you please tell Mr. M at least how long will it take to reach his sweet home?
Input
The input contains multiple test cases.
The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
The second line contains one integer M (0<=M<=10000), which is the number of roads.
The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range
of [1,500].
Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.
To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.
Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.
Output
For each test case, output one integer representing the minimum time to reach home.
If it is impossible to reach home according to Mr. M's demands, output -1 instead.
Sample Input
2 1 1 2 100 1 2 3 3 1 2 100 1 3 40 2 3 50 1 2 1 5 5 3 1 200 5 3 150 2 5 160 4 3 170 4 2 170 1 2 2 2 1 0
Sample Output
100 90 540
原题链接:http://poj.org/problem?id=3767
题意:一个国家有n个城市,之间共有m条路,每座城市属于group1或group2,1一定属于group1 , 2一定属于group2,属于group2 的城市不能直接到达属于group1的城市,问从1到2的最短路。
有人说建图是关键,但我认为,松弛操作时才是关键。建图双向边单向边不好控制。
如果均建双向边,求解的过程中避免直接从group2到group1即可。
理解题意后DIjkstra算法和spfa算法随便来。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=605;
const int INF=0x3f3f3f3f;
int a[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int fa[maxn];
int n,m;
void Dijkstra()
{
for(int i=1; i<=n; i++)
{
dis[i]=a[1][i];
vis[i]=false;
}
dis[1]=0;
vis[1]=true;
for(int i=0; i<n; i++)
{
int minn=INF;
int p=-1;
for(int j=1; j<=n; j++)
{
if(!vis[j]&&dis[j]<minn)
minn=dis[p=j];
}
if(p==-1)
break;
vis[p]=true;
for(int j=1; j<=n; j++)
{
if(fa[p]==2&&fa[j]==1)
continue;
if(!vis[j]&&dis[j]>dis[p]+a[p][j])
dis[j]=dis[p]+a[p][j];
}
}
}
void spfa()
{
for(int i=1; i<=n; i++)
{
dis[i]=INF;
vis[i]=false;
}
dis[1]=0;
vis[1]=true;
queue<int>q;
q.push(1);
while(!q.empty())
{
int p=q.front();
q.pop();
vis[p]=false;
for(int i=1; i<=n; i++)
{
if(fa[p]==2&&fa[i]==1)
continue;
if(dis[i]>dis[p]+a[p][i])
{
dis[i]=dis[p]+a[p][i];
if(!vis[i])
{
vis[i]=true;
q.push(i);
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("data/3767.txt","r",stdin);
while(cin>>n,n)
{
cin>>m;
for(int i=0; i<=n; i++)
{
for(int j=0; j<=n; j++)
if(i==j) a[i][j]=0;
else a[i][j]=INF;
}
int x,y,z;
while(m--)
{
cin>>x>>y>>z;
a[x][y]=a[y][x]=z;
}
for(int i=1; i<=n; i++)
cin>>fa[i];
//Dijkstra();
spfa();
if(dis[2]<INF)
cout<<dis[2]<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
相关文章推荐
- 基于Java实现的Dijkstra算法示例
- Dijkstra和floyd——求单源点最短路径
- 初学图论-Dijkstra单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法基于优先级队列(Priority Queue)的实现
- Dijkstra算法的粗略学习
- Hdu2066(一个人的旅行)
- 【日常练习 dijkstra】POJ 2387 Til the Cows Come Home
- 只有5行的Floyd算法!!!
- 【高手回避】poj3268,一道很水的dijkstra算法题
- poj2387 Til the Cows Come Home—Dijkstra模板
- YEN算法和删除算法分别实现K可靠最短路径算法
- Dijkstra 算法实现及问题
- Djkstra
- 最短路
- 一个人的旅行
- HDU 2544
- pat甲级_路径问题(例题:pat 1003 Emergency (25))
- 文章标题
- CSU1307 并查集+SPFA
- Poj2638 网络流+最短路+二分答案