其实我只是来练练模板熟练度顺便找找快感的
似乎都没有啥改动。。除了2680要再连一下出发地点到公交车站。。
好吧 当我无聊
2544
#include<stdio.h>
#include<string.h>
#include<queue>
#define maxn 110
#define maxm 10010
using namespace std;
int v[maxm],first[maxn],next[maxm],cost[maxm],d[maxm];
typedef pair<int,int> node;
int e;
void add_edge(int a,int b,int c)
{
v[e]=b;
next[e]=first[a];
first[a]=e;
cost[e]=c;
e++;
}
void dijkstra(int s)
{
memset(d,-1,sizeof(d));
priority_queue< node,vector<node>,greater<node> > q;
d[s]=0;
q.push(make_pair(0,s));
while(!q.empty())
{
while(!q.empty()&&q.top().first>d[q.top().second])q.pop();
if(q.empty())break;
int num=q.top().second;
q.pop();
for(int i=first[num];i!=-1;i=next[i])
{
if(d[v[i]]==-1||d[v[i]]>d[num]+cost[i])
{
d[v[i]]=d[num]+cost[i];
q.push(make_pair(d[v[i]],v[i]));
}
}
}
}
int main()
{
int n,m,a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)break;
e=0;
memset(first,-1,sizeof(first));
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
add_edge(b,a,c);
}
dijkstra(1);
printf("%d\n",d[n]);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define maxn 1010
#define maxm 20010
using namespace std;
int first[maxn],d[maxn];
int next[maxm],v[maxm],cost[maxm];
typedef pair <int,int> nd;
int e;
void add_edge(int a,int b,int c)
{
v[e]=b;
next[e]=first[a];
first[a]=e;
cost[e]=c;
e++;
}
void dijkstra(int t)
{
memset(d,-1,sizeof(d));
priority_queue< nd,vector<nd>,greater<nd> > q;
d[t]=0;
q.push(make_pair(0,t));
while(!q.empty())
{
while(!q.empty()&&q.top().first>d[q.top().second])q.pop();
if(q.empty())break;
int num=q.top().second;
q.pop();
for(int i=first[num];i!=-1;i=next[i])
{
if(d[v[i]]==-1||d[v[i]]>d[num]+cost[i])
{
d[v[i]]=d[num]+cost[i];
q.push(make_pair(d[v[i]],v[i]));
}
}
}
}
int main()
{
int n,m,s,p,q,t,w,station;
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
e=0;
memset(first,-1,sizeof(first));
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&p,&q,&t);
add_edge(p,q,t);
}
scanf("%d",&w);
for(int i=0;i<w;i++)
{
scanf("%d",&station);
add_edge(0,station,0);
}
dijkstra(0);
printf("%d\n",d[s]);
}
return 0;
}
186

被折叠的 条评论
为什么被折叠?



