poj之旅——3259,3268
来源:互联网 发布:百合体验知乎 编辑:程序博客网 时间:2024/05/18 05:19
基础模板题。
3259:
#include<cstdio>#include<algorithm>#include<cstring>#define maxm 10000#define INF 0x3f3f3f3fint d[maxm];int cnt,n,m,w;struct Edge{int from,to,dist;}e[maxm];void addedge(int u,int v,int w){e[cnt].from=u;e[cnt].to=v;e[cnt++].dist=w;}bool find_negative_loop(){memset(d,0,sizeof(d));for (int i=0;i<n;i++)for (int j=0;j<cnt;j++){Edge t=e[j];if (d[t.to]>d[t.from]+t.dist){d[t.to]=d[t.from]+t.dist;if (i==n-1)return true;}} return false;}int main(){int T;scanf("%d",&T);while (T--){scanf("%d%d%d",&n,&m,&w);int u,v,cost;cnt=0;for (int i=0;i<m;i++){scanf("%d%d%d",&u,&v,&cost);u--;v--;addedge(u,v,cost);addedge(v,u,cost);}for (int i=0;i<w;i++){scanf("%d%d%d",&u,&v,&cost);u--;v--;addedge(u,v,-cost);}if (find_negative_loop())printf("YES\n");else printf("NO\n");}return 0;}
3268:
#include<cstdio>#include<algorithm>#include<queue>#include<cstring>#include<vector>#define maxn 10000#define INF 0x7f7f7f7fusing namespace std;int n,m,id;struct Edge{int from,to,dist;Edge(int from,int to,int dist):from(from),to(to),dist(dist){}};struct HeapNode{int d,u;bool operator < (HeapNode rhs)const{return d>rhs.d;}HeapNode(int d,int u):d(d),u(u){};};struct Dijkstra{int n,m;vector<Edge> edges;vector<int> G[maxn];bool done[maxn];int d[maxn];int p[maxn];void init (int n){this->n=n;for (int i=0;i<n;i++) G[i].clear();edges.clear();}void addedge(int from,int to,int dist){edges.push_back(Edge(from,to,dist));m=edges.size();G[from].push_back(m-1);}void dijsktra(int s){priority_queue<HeapNode> Q;for (int i=0;i<n;i++) d[i]=INF;d[s]=0;memset(done,0,sizeof(done));Q.push(HeapNode(0,s));while (!Q.empty()){HeapNode x=Q.top();Q.pop();int u=x.u;if (done[u])continue;done[u]=true;for (int i=0;i<G[u].size();i++){Edge e=edges[G[u][i]];if (d[e.to]>d[u]+e.dist){d[e.to]=d[u]+e.dist;p[e.to]=G[u][i];Q.push(HeapNode(d[e.to],e.to));}} }}}G1,G2;int main(){scanf("%d%d%d",&n,&m,&id);id--;G1.init(n);G2.init(n);for (int i=0;i<m;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);u--;v--;G1.addedge(u,v,w);G2.addedge(v,u,w);}G1.dijsktra(id);G2.dijsktra(id);int ans=G1.d[0]+G2.d[0];for (int i=1;i<n;i++)ans=max(ans,G1.d[i]+G2.d[i]);printf("%d",ans);return 0;}
0 0
- poj之旅——3259,3268
- poj之旅——3258
- poj之旅——3273
- poj之旅——3104
- poj之旅——3045
- poj之旅——2976
- poj之旅——3111。
- poj之旅——3579
- poj之旅——1979
- poj之旅——3009
- poj之旅——3669
- poj之旅——2718
- poj之旅——3187
- poj之旅——3050
- poj之旅——2376
- poj之旅——1328
- poj之旅——3190
- poj之旅——2393
- Poj 3659 Perfect Service【最小支配集、树型dp】
- SBTree的左旋右旋以及各种调整操作的基本实现(包含查找第k大的数值)
- xp sp3上VS2010后打开cpp 闪烁刷新现象
- 更换服务器IP地址后,Ambari各项服务心跳丢失(heartbeat lost)的解决方案
- Android动画 -- Property Animation(二)
- poj之旅——3259,3268
- error C2275: 'xxx' : illegal use of this type as an expression
- Metamap Java Api 使用教程
- 翻看谷歌源码 那些让人感兴趣的东西--通知的好用功能
- linux基本命令(45)——free命令
- UVA 10739 [classical dp]
- apache基于域名的虚拟主机配置
- 内存模型—软件硬件结合讲JVM内存模型
- bzoj2960: 跨平面