题目链接
畅通工程续
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 72085 Accepted Submission(s): 27916
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),分别代表起点和终点。
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
Source
2008浙大研究生复试热身赛(2)——全真模拟
Recommend
lcy | We have carefully selected several similar problems for you: 2544 2066 2112 1217 1875
/*
* @Author: Achan
* @Date: 2018-10-26 17:58:34
* @Last Modified by: Achan
* @Last Modified time: 2018-10-26 20:30:03
*/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
#define X first
#define Y second
#define eps 1e-2
#define gcd __gcd
#define pb push_back
#define PI acos(-1.0)
#define lowbit(x) (x)&(-x)
#define fin freopen("in.txt","r",stdin);
#define fout freopen("out.txt","w",stdout);
#define bug printf("!!!!!\n");
#define mem(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<(int)k;i++)
#define per(i,j,k) for(int i=j;i<=(int)k;i++)
#define io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
typedef long long ll;
typedef long double LD;
typedef pair<int,int> pii;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int inf = 1<<30;
const ll INF = 1e18 ;
const int mod = 1e9+7;
const int maxn = 2e2+2;
int head[maxn], dis[maxn];
bool vis[maxn];
struct node
{
int to;
int nex;
int w;
}edge[maxn*maxn];
int top;
int n;
void init() {memset(head,-1,sizeof(head)); top = 0;}
inline void add_edge(int u, int v, int w) //链式向前星图
{
edge[top].to = v;
edge[top].w = w;
edge[top].nex = head[u];
head[u] = top++;
edge[top].to = u;
edge[top].w = w;
edge[top].nex = head[v];
head[v] = top++;
}
void Dijkstra(int s)
{
memset(vis,0,sizeof(vis));
fill(dis,dis+maxn,inf);
priority_queue <pair<ll, int> > q;
q.push(make_pair(0,s));
dis[s] = 0;
while(!q.empty())
{
int cur = q.top().second;
q.pop();
if(vis[cur]) continue;
vis[cur] = 1;
for(int i = head[cur]; ~i; i=edge[i].nex)
{
int w = edge[i].w;
int to = edge[i].to;
if(!vis[to] && dis[to] > dis[cur] + w)
{
dis[to] = dis[cur] + w;
// cout<<"from : "<<cur<<" to :"<<to<<" val :"<<dis[to]<<endl;
q.push(make_pair(-dis[to], to));
}
}
}
}
int main(void)
{
int m;
while(cin>>n>>m)
{
init();
int u,v,w,s,t;
while(m--)
{
// cin>>u>>v>>w;
u = read() , v = read(), w = read();
add_edge(u,v,w);
}
// for(int i = 0 ;i<top; i++)
// cout<<edge[i].nex<<" "<<edge[i].to<<" "<<edge[i].w<<endl;
cin>>s>>t;
Dijkstra(s);
cout<<( dis[t] == inf? -1:dis[t])<<endl;
}
}
Spfa
/*
* @Author: Achan
* @Date: 2018-10-26 17:58:34
* @Last Modified by: Achan
* @Last Modified time: 2018-10-29 17:38:41
*/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
#define X first
#define Y second
#define eps 1e-2
#define gcd __gcd
#define pb push_back
#define PI acos(-1.0)
#define lowbit(x) (x)&(-x)
#define fin freopen("in.txt","r",stdin);
#define fout freopen("out.txt","w",stdout);
#define bug printf("!!!!!\n");
#define mem(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<(int)k;i++)
#define per(i,j,k) for(int i=j;i<=(int)k;i++)
#define io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
typedef long long ll;
typedef long double LD;
typedef pair<int,int> pii;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int inf = 1<<30;
const ll INF = 1e18 ;
const int mod = 1e9+7;
const int maxn = 2e2+2;
int head[maxn], dis[maxn];
bool vis[maxn];
struct node
{
int to;
int nex;
int w;
}edge[maxn*maxn];
int top;
int n;
void init() {memset(head,-1,sizeof(head)); top = 0;}
inline void add_edge(int u, int v, int w) //链式向前星图
{
edge[top].to = v;
edge[top].w = w;
edge[top].nex = head[u];
head[u] = top++;
edge[top].to = u;
edge[top].w = w;
edge[top].nex = head[v];
head[v] = top++;
}
void spfa(int s)
{
memset(vis,0,sizeof(vis));
fill(dis,dis+maxn,inf);
dis[s] = 0;
vis[s] = 1;
queue <int> q;
q.push(s);
while(!q.empty())
{
int cur = q.front();
q.pop();
vis[cur] = 0; //从队列中pop出去的 设置可访问态,可以再次用于更新别的最短路!!!
for(int i=head[cur]; ~i; i=edge[i].nex)
{
int w = edge[i].w;
int to = edge[i].to;
if(dis[to] > dis[cur] + w)
{
dis[to] = dis[cur] + w;
if(!vis[to])
{
vis[to] = 1;
q.push(to);
}
}
}
}
}
int main(void)
{
// fout
io
int m;
while(cin>>n>>m)
{
init();
int u,v,w,s,t;
while(m--)
{
// cin>>u>>v>>w;
u = read() , v = read(), w = read();
add_edge(u,v,w);
}
// for(int i = 0 ;i<top; i++)
// cout<<edge[i].nex<<" "<<edge[i].to<<" "<<edge[i].w<<endl;
cin>>s>>t;
spfa(s);
cout<<( dis[t] == inf? -1:dis[t])<<endl;
}
}