hdu 1874 畅通工程续
来源:互联网 发布:这位程序员四十岁了还 编辑:程序博客网 时间:2024/05/17 03:55
这道题没有1A,因为没有考虑重边。。。
floyd可以过。。
#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<iostream>#include<queue>#include<vector>using namespace std;int way[210][210];int main(){ int n,m; while(cin>>n>>m) { int a,b,x; memset(way,0x3f3f3f3f,sizeof(way)); for(int i=0;i<n;i++) { way[i][i]=0; } while(m--) { cin>>a>>b>>x; way[a][b]=min(way[a][b],x); way[b][a]=min(way[b][a],x); } for(int k=0;k<n;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) way[i][j]=min(way[i][j],way[i][k]+way[k][j]); cin>>a>>b; if(way[a][b]==0x3f3f3f3f) cout<<"-1"<<endl; else cout<<way[a][b]<<endl; } return 0;}
然后,djstl的话,就根本不用担心重边什么的。。
#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<iostream>#include<queue>#include<vector>using namespace std;struct sb{ int x; int t; sb(int a,int b) { x=a; t=b; } bool operator<(const sb&wakaka)const { return t>wakaka.t; }};int n,m;vector<sb>bian[210];int ts[210];bool vis[210];void djstl(int who){ for(int i=0;i<n;i++) { ts[i]=0x3f3f3f3f; vis[i]=false; } ts[who]=0; priority_queue<sb>q; q.push(sb(who,ts[who])); while(!q.empty()) { sb x=q.top(); q.pop(); if(vis[x.x]) { continue; } vis[x.x]=true; for(int i=0;i<bian[x.x].size();i++) { sb y=bian[x.x][i]; if(ts[y.x]>x.t+y.t) { ts[y.x]=x.t+y.t; q.push(sb(y.x,ts[y.x])); } } }}int main(){ while(cin>>n>>m) { int a,b,x; while(m--) { cin>>a>>b>>x; bian[a].push_back(sb(b,x)); bian[b].push_back(sb(a,x)); } cin>>a>>b; djstl(a); if(ts[b]==0x3f3f3f3f) { cout<<"-1"<<endl; } else { cout<<ts[b]<<endl; } for(int i=0;i<n;i++) { bian[i].clear(); } } return 0;}
0 0
- HDU 1874 畅通工程续
- hdu 1874 畅通工程续
- hdu 1874 畅通工程续
- hdu 1874 畅通工程续
- HDU 1874 畅通工程续
- HDU 1874 畅通工程续
- HDU 1874 畅通工程续
- HDU 1874 畅通工程续
- HDU 1874 畅通工程续
- hdu 1874 畅通工程续
- hdu 1874 畅通工程续
- hdu 1874 畅通工程续
- hdu 1874 畅通工程续
- HDU 1874 畅通工程续
- hdu 1874 畅通工程续
- HDU 1874 畅通工程续
- HDU 1874 畅通工程续
- hdu 1874(畅通工程续)
- 四个数的加减乘除
- 链表,栈和队列
- POJ 3159 差分约束系统+spfa
- Python按位运算符,字典,运算符优先级的基本总结
- linux TCP 和 socket 参数设置
- hdu 1874 畅通工程续
- Java异常处理方案【总结篇】
- 【LeetCode】Delete Node in a Linked List 解题报告
- 机器学习向量化练习
- ThoughtWorks的一道面试题
- 21. Merge Two Sorted Lists
- 解决mysql 不能插入中文 编码问题
- POJ 1275 差分约束系统+二分
- 记录PHP错误日志 display_errors与log_errors的区别