poj 3662
代码:
#include<iostream>
#include<fstream>
#include<queue>
using namespace std;
int n,m,l;
struct e{
int data;
int cost;
e *next;
};
e edge[1001];
void add(int s,int t ,int w){
e *p=new e;
p->data=s;
p->cost=w;
p->next=edge[t].next;
edge[t].next=p;
e *q=new e;
q->data=t;
q->cost=w;
q->next=edge[s].next;
edge[s].next=q;
}
struct node{
int s,t;
int w;
friend bool operator<(node a,node b){
return a.w>b.w;
}
};
int d[1001][1001];
int v[1001][1001];
void solve(){
int i,j,k,s,t;
priority_queue<node> q;
node a;
for(i=0;i<=l;i++)
for(j=1;j<=n;j++)
d[i][j]=10000000;
a.s=0;
a.t=1;
d[0][1]=0;
a.w=0;
q.push(a);
while(!q.empty())
{
a=q.top();
q.pop();
s=a.s;
t=a.t;
if(s==l&&t==n)
{
cout<<a.w<<endl;
return;
}
if(v[s][t]) continue;
v[s][t]=1;
e *p=edge[t].next;
while(p)
{
if(d[s][p->data]>max(a.w,p->cost))
{
d[s][p->data]=max(a.w,p->cost);
node b;
b.s=s;
b.t=p->data;
b.w=d[s][p->data];
q.push(b);
}
if(s<l&&d[s+1][p->data]>a.w)
{
d[s+1][p->data]=a.w;
node b;
b.s=s+1;
b.t=p->data;
b.w=d[s+1][p->data];
q.push(b);
}
p=p->next;
}
}
cout<<-1<<endl;
}
void read(){
// ifstream cin("in.txt");
int i,j,k,s,t,w;
cin>>n>>m>>l;
for(i=1;i<=m;i++)
{
cin>>s>>t>>w;
add(s,t,w);
}
solve();
}
int main(){
read();
return 0;
}
浙公网安备 33010602011771号