您的位置:首页 > 其它

【codevs 1557】热浪

2017-07-15 23:14 316 查看
题目描述 Description

德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品。Farmer John此时以先天下之忧而忧,后天下之乐而乐的精神,身先士卒地承担起向德克萨斯运送大量的营养冰凉的牛奶的重任,以减轻德克萨斯人忍受酷暑的痛苦。

FJ已经研究过可以把牛奶从威斯康星运送到德克萨斯州的路线。这些路线包括起始点和终点先一共经过T (1 <= T <= 2,500)个城镇,方便地标号為1到T。除了起点和终点外地每个城镇由两条双向道路连向至少两个其它地城镇。每条道路有一个通过费用(包括油费,过路费等等)。

给定一个地图,包含C (1 <= C <= 6,200)条直接连接2个城镇的道路。每条道路由道路的起点Rs,终点Re (1 <= Rs <= T; 1 <= Re <= T),和花费(1 <= Ci <= 1,000)组成。求从起始的城镇Ts (1 <= Ts <= T)到终点的城镇Te(1 <= Te <= T)最小的总费用。

输入描述 Input Description

第一行: 4个由空格隔开的整数: T, C, Ts, Te

第2到第C+1行: 第i+1行描述第i条道路。有3个由空格隔开的整数: Rs, Re和Ci

输出描述 Output Description

一个单独的整数表示从Ts到Te的最小总费用。数据保证至少存在一条道路。

样例输入 Sample Input

7 11 5 4

2 4 2

1 4 3

7 2 2

3 4 3

5 7 5

7 3 3

6 1 1

6 3 4

2 4 3

5 6 3

7 2 1

样例输出 Sample Output

7

spfa裸题

记得双向边<<1

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=10086;
int first[maxn<<1],next[maxn<<1],dist[maxn<<1],tot;
int n,m,s,e;
bool used[maxn];
struct edge
{
int f,t,v;
}l[maxn<<1];
void build(int f,int t,int v)
{
l[++tot]=(edge){f,t,v};
next[tot]=first[f];
first[f]=tot;
return;
}
void spfa(int s)
{
queue <int> q;
q.push(s);
used[s]=1;
dist[s]=0;
while(!q.empty())
{
int f=q.front();
q.pop();
used[f]=0;
for(int i=first[f];i;i=next[i])
{
int v=l[i].t;
if(dist[v]>dist[f]+l[i].v)
{
dist[v]=dist[f]+l[i].v;
if(!used[v])
{
q.push(v);
used[v]=1;
}
}
}
}
}
int main()
{
int a,b,c;
scanf("%d%d%d%d",&n,&m,&s,&e);
memset(dist,0x3f,sizeof(dist));//必须初始化
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
build(a,b,c);
build(b,a,c);
}
spfa(s);
printf("%d",dist[e]);
cout<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: