POJ -- 2387 Til the Cows Come Home
来源:互联网 发布:少女前线机枪数据 编辑:程序博客网 时间:2024/06/08 08:43
代码实现:
SPFA:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int dis[1010],visit[1010],cnt[1010],top,n,t;struct Edge{ int num,val; Edge *next; Edge(int n=0,int v=0,Edge *p=0):num(n),val(v),next(p){}}*h[1010],e[4010];void Addedge(int from,int to,int v){ Edge *p=&e[top++]; p->num=to; p->val=v; p->next=h[from]; h[from]=p;}queue<Edge>q;void SPFA(){ while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) dis[i]=1e9; dis[1]=0; memset(visit,0,sizeof(visit)); memset(cnt,0,sizeof(cnt)); visit[1]=1; Edge head(1,0); Edge next; q.push(head); while(!q.empty()){ head=q.front(); q.pop(); visit[head.num]=0; //printf("*%d %d\n",head.num,head.val); for(Edge *p=h[head.num];p;p=p->next){ next=*p; if(dis[next.num]>dis[head.num]+next.val){ dis[next.num]=dis[head.num]+next.val; if(!visit[next.num]){ cnt[next.num]++; if(cnt[next.num]>=n) return; visit[next.num]=1; q.push(next); } } } }}int main(){ while(~scanf("%d%d",&t,&n)){ top=0; memset(h,0,sizeof(h)); int v1,v2,v; while(t--){ scanf("%d%d%d",&v1,&v2,&v); Addedge(v1,v2,v); Addedge(v2,v1,v); } SPFA(); printf("%d\n",dis[n]); }}
Dijkatra:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int dis[1010],visit[1010],cnt[1010],top,n,t;struct Edge{ int num,val; Edge *next; Edge(int n=0,int v=0,Edge *p=0):num(n),val(v),next(p){} bool operator<(const Edge& a)const{ return val>a.val; }}*h[1010],e[4010];void Addedge(int from,int to,int v){ Edge *p=&e[top++]; p->num=to; p->val=v; p->next=h[from]; h[from]=p;}void Dijkstra(){ priority_queue<Edge> q; for(int i=1;i<=n;i++) dis[i]=1e9; dis[1]=0; memset(visit,0,sizeof(visit)); q.push(Edge(1,0)); while(!q.empty()){ int x=q.top().num; q.pop(); if(visit[x]==1) continue; visit[x]=1; if(x==n) break; for(Edge *p=h[x];p;p=p->next){ if(!visit[p->num]&&dis[p->num]>dis[x]+p->val){ dis[p->num]=dis[x]+p->val; q.push(Edge(p->num,dis[p->num])); } } }}int main(){ while(~scanf("%d%d",&t,&n)){ top=0; memset(h,0,sizeof(h)); int v1,v2,v; while(t--){ scanf("%d%d%d",&v1,&v2,&v); Addedge(v1,v2,v); Addedge(v2,v1,v); } Dijkstra(); printf("%d\n",dis[n]); }}
0 0
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- Poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ-2387-Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- Face Detection and Tracking Using the KLT Algorithm
- hadoop集群中的日志文件
- 初次接触FTPclient
- c- Ubiquitous Religions
- 移动共享开发(二)各平台SSO(免登录)配置 微信和微信朋友圈、QQ、QQ空间、新浪微博、腾讯微博、人人
- POJ -- 2387 Til the Cows Come Home
- c#格式化说明总结
- org.apache.jasper.JasperException: /index.jsp (line: 24, column: 26) attribute for %>" is not proper
- bootstrap入门【按钮和图片】
- Scala类型 11: Upper Bounds & Lower Bounds
- how to update facebook share link, titile, description
- monkeyrunner 可执行
- 笔记本出现“默认网关不可用”
- D - The Suspects