[图论训练]BZOJ 3245: 最快路线【最短路】

6 15 1
0 1 25 68
0 2 30 50
0 5 0 101
1 2 70 77
1 3 35 42
2 0 0 22
2 1 40 86
2 3 0 23
2 4 45 40
3 1 64 14
3 5 0 23
4 1 95 8
5 1 0 84
5 2 90 64
5 3 36 40

0 5 2 3 1

HINT

【数据范围】

30% N<=20

100% 2<=N<=150;0<=V<=500;1<=L<=500

Source

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#define maxn 100000
using namespace std;
typedef pair<int, int> pii;
pii pre[500][540];
int visit[500][540];
double dist[500][540];
int ans[100000];
void add(int x,int y,int s,int l)
{
point[now] = y;
len[now] = l;
speed[now] = s;
}
void spfa(int s)
{
for(int i=0;i<=199;i++)
for(int j = 0; j <= 539; j++)dist[i][j] = -1;
dist[s][70] = 0;
visit[s][70] = 1;
queue<pii>q;
q.push(make_pair(s, 70));
while(!q.empty())
{
pii k = q.front();
q.pop();
visit[k.first][k.second] = 0;
for(int i = head[k.first]; i; i = nex[i])
{
int u = point[i], sp;
double di = 0;
if(speed[i] != 0)
{
di = dist[k.first][k.second] + 1.0*len[i] / speed[i], sp = speed[i];
}
else
{
di = dist[k.first][k.second] + 1.0*len[i] / k.second, sp = k.second;
}
if(di < dist[u][sp] || dist[u][sp] == -1)
{
dist[u][sp] = di;
pre[u][sp] = k;
if(!visit[u][sp])
{
visit[u][sp] = 1;
q.push(make_pair(u,sp));
}
}
}
}
}
int main()
{
int n,m,d,x,y,a,b,h=0;
scanf("%d%d%d",&n,&m,&d);
d++;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d%d",&x,&y,&a,&b);
add(x + 1, y + 1, a, b);
}
spfa(1);
double an = 1000000000.00;
for(int i = 0; i <= 500; i++)if(dist[d][i] != -1)
{
if(dist[d][i] < an)
{
an = dist[d][i];
y = i;
}
}
x = d;
ans[++h] = d;
while(pre[x][y].first != 0 || pre[x][y].second != 0)
{
ans[++h] = pre[x][y].first;
int xx = pre[x][y].first, yy = pre[x][y].second;
x = xx; y = yy;
}
for(int i = h; i >= 2; i--)
{
printf("%d ",ans[i]-1);
}
printf("%d\n",ans[1]-1);
return 0;
}

posted @ 2015-08-17 23:50  philippica  阅读(740)  评论(0编辑  收藏  举报