畅通工程续
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23601 Accepted Submission(s): 8310
Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
Sample Input
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
Sample Output
2 -1
Author
linle
Dijkstra(优先队列)
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<functional>
#include<vector>
#include<queue>
#define MAX 1005
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{ //每条边的属性
int from, to, cost;
Edge(int f, int t, int c) :from(f), to(t), cost(c){}
};
int N, M;
vector<Edge>edge[MAX]; //创建存储边的数组
int Mincost[MAX];
typedef pair<int, int >P; //将最短的边的长度,跟点通过pair作为一个整体 注意:长度做first,节点做second
void Dijkstra(int s){
priority_queue<P, vector<P>, greater<P> >pq; //最小值优先出对的优先队列
fill(Mincost, Mincost + MAX, INF); //将所有的点的最短距离初始化为INF
//fill(Mincost,Mincost+M,INF);
Mincost[s] = 0; pq.push(P(0, s)); //起点的最短距离设为0,将(0,s)压入
while (!pq.empty()){
P temp = pq.top(); pq.pop();
int tempnode = temp.second; //tempnode 接受点的信息
if (Mincost[tempnode] != temp.first) continue; //如果这个点的最短距离不等于.first 继续循环
for (int i = 0; i<edge[tempnode].size(); i++){ //依次遍历以这个点为起点的边
Edge e = edge[tempnode][i];
if (Mincost[e.to]>Mincost[tempnode] + e.cost){//如果某点的距离因为别的点变小,则改变(比如,某点开始时无法到达起点,但后来因为某点加入导致距离变小,则需改变)
Mincost[e.to] = Mincost[tempnode] + e.cost;
pq.push(P(Mincost[e.to], e.to));//压入改变的点
}
}
}
}
int main(){
while (cin >> N >> M){
memset(edge, 0, sizeof(edge));
for (int i = 0; i<M; i++){
int A, B, C; cin >> A >> B >> C;
Edge e(A, B, C);
Edge e2(B, A, C);
//无向图,各为起点
edge[A].push_back(e); //以A为起点的边
edge[B].push_back(e2); //以B为起点的边
}
int s, t; cin >> s >> t;
Dijkstra(s);
if (Mincost[t] != INF)
cout << Mincost[t] << endl;
else cout << -1 << endl;
}
}
Djikstra
#include<iostream>
#include<cstdlib>
#include<cstring>
#define INF 0x3f3f3f3f
#define N 205
using namespace std;
int n, m;
int Map[N][N], Mincost[N];
int visit[N];
void Init(){
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
Map[i][j] = INF;
}
}
}
void Dijkstra(int s){
int Min, post;
memset(visit, 0, sizeof(visit));//所有点都为选过
for (int i = 0; i < n; i++){
Mincost[i] = Map[s][i];//与s相接的点的长度
}
visit[s] = 1; Mincost[s] = 0;//s标记,及本点的距离为0
for (int i = 0; i<n; i++){
Min = INF;
for (int j = 0; j<n; j++){
if (!visit[j] && Min>Mincost[j]){
Min = Mincost[j];
post = j;
}
}
if (Min != INF){
visit[post] = 1;
for (int j = 0; j<n; j++){
if (!visit[j] && Mincost[j]>Mincost[post] + Map[post][j]){//更改距离
Mincost[j] = Mincost[post] + Map[post][j];
}
}
}
}
}
int main(){
while (cin >> n >> m){
Init();
for (int i = 0; i<m; i++){
int from, to, cost;
cin >> from >> to >> cost;
if (Map[from][to]>cost){//很关键,在此wa了好几次,,作用:防止重边
Map[from][to] = cost;
Map[to][from] = cost;
}
}
int s, t; cin >> s >> t;
Dijkstra(s);
if (s == t) cout << 0 << endl;
else if (Mincost[t] != INF){
cout << Mincost[t] << endl;
}
else cout << -1 << endl;
}
}
Floyd
#include<iostream>
#include<cstdlib>
#include<cstring>
#define MAX 205
#define INF 10000005
using namespace std;
int n,m,Map[MAX][MAX];
void Init(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
Map[i][j]=(i==j)?0:INF;
}
}
}
void Floyd(){
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
//if(Map[i][j]<INF && Map[k][j]<INF)
Map[i][j]=min(Map[i][j],Map[i][k]+Map[k][j]);
}
}
}
}
int main(){
while(cin>>n>>m){
Init();
for(int i=0;i<m;i++){
int a,b,x;
cin>>a>>b>>x;
if(Map[a][b]>x){
Map[a][b]=Map[b][a]=x;
}
}
int s,t; cin>>s>>t;
Floyd();
if(Map[s][t]<INF){
cout<<Map[s][t]<<endl;
}
else cout<<-1<<endl;
}
}
SPFA
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<queue>
#define MAX 205
#define INF 0x3f3f3f3f
using namespace std;
int Map[MAX][MAX],Mincost[MAX],visit[MAX];
int n,m;
void Inite(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
Map[i][j]=INF;
}
}
}
void SPFA(int s){
for(int i=0;i<n;i++){
Mincost[i]=INF;
}
memset(visit,0,sizeof(visit));
Mincost[s]=0,visit[s]=1;
queue<int>q; q.push(s);
while(!q.empty()){
int temp=q.front(); q.pop();
visit[temp]=0;
for(int i=0;i<n;i++){
if(Mincost[i]>Mincost[temp]+Map[temp][i]){
Mincost[i]=Mincost[temp]+Map[temp][i];
if(!visit[i]){
visit[i]=1;
q.push(i);
}
}
}
}
}
int main(){
while(cin>>n>>m){
Inite();
for(int i=0;i<m;i++){
int a,b,c; cin>>a>>b>>c;
if(Map[a][b]>c){
Map[a][b]=Map[b][a]=c;
}
}
int s,t; cin>>s>>t;
SPFA(s);
if(Mincost[t]!=INF){
cout<<Mincost[t]<<endl;
}
else cout<<-1<<endl;
}
}
SPFA (邻接表)
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<queue>
#define MAX 205
#define MAX_W 1005
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
int from, to, cost;
Edge(int f, int t, int c) :from(f), to(t), cost(c){}
};
vector<Edge>edge[MAX_W];
int Mincost[MAX];
int n, m, visit[MAX];
void SPFA(int s){
for (int i = 0; i<MAX; i++){
Mincost[i] = INF;
}
memset(visit, 0, sizeof(visit));
visit[s] = 1, Mincost[s] = 0;
queue<int>q; q.push(s);
while (!q.empty()){
int tempnode = q.front(); q.pop();
visit[tempnode] = 0;
for (int i = 0; i<edge[tempnode].size(); i++){
Edge tempE = edge[tempnode][i];
int id = tempE.to;
if (Mincost[id]>Mincost[tempnode] + tempE.cost){
Mincost[id] = Mincost[tempnode] + tempE.cost;
if (!visit[id]){
visit[id] = 1;
q.push(id);
}
}
}
}
}
int main(){
while (cin >> n >> m){
memset(edge, 0, sizeof(edge));
for (int i = 0; i<m; i++){
int a, b, c; cin >> a >> b >> c;
Edge e(a, b, c);
Edge e2(b, a, c);
edge[a].push_back(e);
edge[b].push_back(e2);
}
int s, t; cin >> s >> t;
SPFA(s);
if (Mincost[t] != INF){
cout << Mincost[t] << endl;
}
else cout << -1 << endl;
}
}